Java读取文件内容
1.Scanner
try(Scanner scanner = new Scanner(new FileReader(fileName))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
2.Files.lines
Stream<String> lines = Files.lines(Paths.get(fileName));
// 随机行顺序进行数据处理
lines.forEach(ele -> {
System.out.println(ele);
});
// 顺序读取数据
lines.forEachOrdered(System.out::println);
3.经典管道流的方式
try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))){
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}