public class Demo03Test {
public static void main(String[] args) throws IOException {
Path path = Paths.get("D:\\aa.txt");
byte [] data = Files.readAllBytes(path);
String result = new String(data , "utf-8");
System.out.println(result);
}
}
文件举例
输出:
还有readline()的方法
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("D:\\aa.txt"));
String line;
while((line = br.readLine())!=null) {
System.out.println(line);
}
br.close();
}
}
代码来源
https://www.zhihu.com/question/67344572