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学习笔记3