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

public class ReadFileExample {
    public static void main(String[] args) {
        String fileName = "C:\\a.txt";
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这个代码使用了Java的IO库,其中BufferedReaderFileReader类用于读取文件中的内容,try-with-resources语句用于自动关闭文件资源,避免资源泄漏。最后,通过System.out.println()输出读取到的内容。

用java写一个读取C盘中atxt文件并将其输出到标准输出的代码

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

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