有很多方法来读一个文本文件在Java. 让我们看看Java读文本文件的不同方法一个接一个。
Java 阅读文本文件
There are many ways to read a text file in java. A text file is made of characters, so we can use Reader classes. There are some utility classes too to read a text file in java.
- 使用 Java 阅读文本文件使用 Files class
- 使用 Java 读文本文件使用
FileReader
- 使用 Java 读文本文件使用 BufferedReader
- 使用 Scanner class读文本文件使用 java
现在让我们看看示例如何使用这些类来读取Java中的文本文件。
Java 使用「java.nio.file.Files」读取文本文件
我们可以使用Files类来读取文件的所有内容到一个字节数组。Files类还有一种方法来读取所有行到一个字符串列表。Files类在Java 7中引入,如果你想加载所有文件内容是好的。
1String fileName = "/Users/pankaj/source.txt";
2Path path = Paths.get(fileName);
3byte[] bytes = Files.readAllBytes(path);
4List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
使用java.io.FileReader读取Java文本文件
您可以使用FileReader来获取BufferedReader,然后读取文件一行一行.FileReader不支持编码,并且与系统默认编码一起工作,所以它不是在Java中读取文本文件的非常有效的方法。
1String fileName = "/Users/pankaj/source.txt";
2File file = new File(fileName);
3FileReader fr = new FileReader(file);
4BufferedReader br = new BufferedReader(fr);
5String line;
6while((line = br.readLine()) != null){
7 //process the line
8 System.out.println(line);
9}
Java 使用 Java.io.BufferedReader 读取文本文件
BufferedReader 是很好的,如果你想读字符串的文件并处理它们. 它是很好的处理大文件,它也支持编码。 BufferedReader 是同步的,所以在 BufferedReader 上的读取操作可以安全地从多个线程进行。
1String fileName = "/Users/pankaj/source.txt";
2File file = new File(fileName);
3FileInputStream fis = new FileInputStream(file);
4InputStreamReader isr = new InputStreamReader(fis, cs);
5BufferedReader br = new BufferedReader(isr);
6
7String line;
8while((line = br.readLine()) != null){
9 //process the line
10 System.out.println(line);
11}
12br.close();
使用扫描器读取Java文本文件
如果您想根据某些 java 常规表达式读取文件的行或行,则 Scanner 是使用的类别。 Scanner 使用分界器模式将其输入分解为代币,默认情况下匹配白空间。 由此产生的代币可以通过使用不同的下一个方法转换为不同类型的值。
1Path path = Paths.get(fileName);
2Scanner scanner = new Scanner(path);
3System.out.println("Read text file using Scanner");
4//read line by line
5while(scanner.hasNextLine()){
6 //process each line
7 String line = scanner.nextLine();
8 System.out.println(line);
9}
10scanner.close();
Java 阅读文件示例
以下是示例类,说明如何在 java 中阅读文本文件. 示例方法是使用扫描器,文件,支持编码的BufferedReader和FileReader。
1package com.journaldev.files;
2
3import java.io.BufferedReader;
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.FileReader;
7import java.io.IOException;
8import java.io.InputStreamReader;
9import java.nio.charset.Charset;
10import java.nio.charset.StandardCharsets;
11import java.nio.file.Files;
12import java.nio.file.Path;
13import java.nio.file.Paths;
14import java.util.List;
15import java.util.Scanner;
16
17public class JavaReadFile {
18
19 public static void main(String[] args) throws IOException {
20 String fileName = "/Users/pankaj/source.txt";
21
22 //using Java 7 Files class to process small files, get complete file data
23 readUsingFiles(fileName);
24
25 //using Scanner class for large files, to read line by line
26 readUsingScanner(fileName);
27
28 //read using BufferedReader, to read line by line
29 readUsingBufferedReader(fileName);
30 readUsingBufferedReaderJava7(fileName, StandardCharsets.UTF_8);
31 readUsingBufferedReader(fileName, StandardCharsets.UTF_8);
32
33 //read using FileReader, no encoding support, not efficient
34 readUsingFileReader(fileName);
35 }
36
37 private static void readUsingFileReader(String fileName) throws IOException {
38 File file = new File(fileName);
39 FileReader fr = new FileReader(file);
40 BufferedReader br = new BufferedReader(fr);
41 String line;
42 System.out.println("Reading text file using FileReader");
43 while((line = br.readLine()) != null){
44 //process the line
45 System.out.println(line);
46 }
47 br.close();
48 fr.close();
49
50 }
51
52 private static void readUsingBufferedReader(String fileName, Charset cs) throws IOException {
53 File file = new File(fileName);
54 FileInputStream fis = new FileInputStream(file);
55 InputStreamReader isr = new InputStreamReader(fis, cs);
56 BufferedReader br = new BufferedReader(isr);
57 String line;
58 System.out.println("Read text file using InputStreamReader");
59 while((line = br.readLine()) != null){
60 //process the line
61 System.out.println(line);
62 }
63 br.close();
64
65 }
66
67 private static void readUsingBufferedReaderJava7(String fileName, Charset cs) throws IOException {
68 Path path = Paths.get(fileName);
69 BufferedReader br = Files.newBufferedReader(path, cs);
70 String line;
71 System.out.println("Read text file using BufferedReader Java 7 improvement");
72 while((line = br.readLine()) != null){
73 //process the line
74 System.out.println(line);
75 }
76 br.close();
77 }
78
79 private static void readUsingBufferedReader(String fileName) throws IOException {
80 File file = new File(fileName);
81 FileReader fr = new FileReader(file);
82 BufferedReader br = new BufferedReader(fr);
83 String line;
84 System.out.println("Read text file using BufferedReader");
85 while((line = br.readLine()) != null){
86 //process the line
87 System.out.println(line);
88 }
89 //close resources
90 br.close();
91 fr.close();
92 }
93
94 private static void readUsingScanner(String fileName) throws IOException {
95 Path path = Paths.get(fileName);
96 Scanner scanner = new Scanner(path);
97 System.out.println("Read text file using Scanner");
98 //read line by line
99 while(scanner.hasNextLine()){
100 //process each line
101 String line = scanner.nextLine();
102 System.out.println(line);
103 }
104 scanner.close();
105 }
106
107 private static void readUsingFiles(String fileName) throws IOException {
108 Path path = Paths.get(fileName);
109 //read file to byte array
110 byte[] bytes = Files.readAllBytes(path);
111 System.out.println("Read text file using Files class");
112 //read file to String list
113 @SuppressWarnings("unused")
114 List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
115 System.out.println(new String(bytes));
116 }
117
118}
选择使用扫描器或BufferedReader或文件读取文件取决于您的项目要求. 例如,如果你只是在登录文件,你可以使用文件和BufferedReader. 如果你要根据分界器来解析文件,你应该使用扫描器类别。 在我结束本教程之前,我想提到关于 RandomAccessFile
。
1RandomAccessFile file = new RandomAccessFile("/Users/pankaj/Downloads/myfile.txt", "r");
2String str;
3
4while ((str = file.readLine()) != null) {
5 System.out.println(str);
6}
7file.close();
这就是 java 阅读文本文件示例程序的全部。