欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java 读取文件全部内容

程序员文章站 2022-07-09 22:34:44
...

主要代码:

import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class Application {
    public static void main(String[] args) throws IOException {
        String content = "";
        StringBuilder builder = new StringBuilder();

        File file = new File("E:\\hello.txt");
        InputStreamReader streamReader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
        BufferedReader bufferedReader = new BufferedReader(streamReader);

        while ((content = bufferedReader.readLine()) != null) 
            builder.append(content);

        System.out.println(builder.toString());
    }
}
相关标签: Java