java逐行读取文件
Today we will look into different java read file line by line methods. Sometimes we have to read file line by line to a String, for example calling a method by passing each line as String to process it.
今天,我们将逐行研究不同的Java读取文件。 有时我们必须逐行读取文件到String,例如,通过将每一行作为String传递来处理它来调用方法。
Java逐行读取文件 (Java Read File line by line)
We can read file line by line using different ways. Let’s look at some of them.
我们可以使用不同的方式逐行读取文件。 让我们看看其中的一些。
Java使用BufferedReader逐行读取文件 (Java Read File line by line using BufferedReader)
We can use java.io.BufferedReader readLine() method to read file line by line to String. This method returns null when end of file is reached. Below is a simple program showing example for java read file line by line using BufferedReader.
我们可以使用java.io.BufferedReader readLine()方法逐行读取文件到String。 到达文件末尾时,此方法返回null。 下面是一个简单的程序,显示了使用BufferedReader逐行读取Java文件的示例。
package com.journaldev.readfileslinebyline;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFileLineByLineUsingBufferedReader {
public static void main(String[] args) {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(
"/Users/pankaj/Downloads/myfile.txt"));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
// read next line
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java使用Scanner逐行读取文件 (Java Read File line by line using Scanner)
We can use Scanner class to open a file and then read its content line by line. Below is the scanner example to read file line by line and print it.
我们可以使用Scanner类打开文件,然后逐行读取其内容。 以下是扫描仪示例,用于逐行读取文件并进行打印。
package com.journaldev.readfileslinebyline;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFileLineByLineUsingScanner {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(new File("/Users/pankaj/Downloads/myfile.txt"));
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Java使用文件逐行读取文件 (Java Read File line by line using Files)
java.nio.file.Files is a utility class that contains various useful methods. Files readAllLines method can be used to read all the file lines into a list of string.
java.nio.file.Files是一个实用程序类,其中包含各种有用的方法。 文件readAllLines方法可用于将所有文件行读入字符串列表 。
package com.journaldev.readfileslinebyline;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class ReadFileLineByLineUsingFiles {
public static void main(String[] args) {
try {
List<String> allLines = Files.readAllLines(Paths.get("/Users/pankaj/Downloads/myfile.txt"));
for (String line : allLines) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java使用RandomAccessFile逐行读取文件 (Java Read File line by line using RandomAccessFile)
We can use RandomAccessFile to open a file in read mode and then use its readLine method to read file line by line.
我们可以使用RandomAccessFile以读取模式打开文件,然后使用其readLine方法逐行读取文件。
package com.journaldev.readfileslinebyline;
import java.io.IOException;
import java.io.RandomAccessFile;
public class ReadFileLineByLineUsingRandomAccessFile {
public static void main(String[] args) {
try {
RandomAccessFile file = new RandomAccessFile("/Users/pankaj/Downloads/myfile.txt", "r");
String str;
while ((str = file.readLine()) != null) {
System.out.println(str);
}
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
That’s all for java read file line by line using different methods.
这就是使用不同方法逐行读取Java文件的全部内容。
References:
参考文献:
翻译自: https://www.journaldev.com/709/java-read-file-line-by-line
java逐行读取文件
3万+

被折叠的 条评论
为什么被折叠?



