代码示例:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        try (BufferedReader reader = new BufferedReader(new FileReader("weather.txt"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

其中,"weather.txt"为文件路径,需要根据实际情况进行修改。运行程序后,控制台将输出文件中每一行的内容

Java用BufferdReader类按行读取文件并输出到控制台上。‎ 天气预报:北京晴上海多云有小雨广州晴有时多云‎‎

原文地址: http://www.cveoy.top/t/topic/gIpH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录