如何用Java逐行读取一个大的文本文件?

我需要用Java来逐行读取一个大约5-6GB的大文本文件。

我怎样才能快速做到这一点?

看看这个博客。

可以指定缓冲区的大小,或者 可以使用默认大小。默认的 默认大小对于大多数 的目的。

// Open the file
FileInputStream fstream = new FileInputStream("textfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

String strLine;

//Read File Line By Line
while ((strLine = br.readLine()) != null)   {
  // Print the content on the console
  System.out.println (strLine);
}

//Close the input stream
fstream.close();
评论(4)

你可以使用扫描器类

Scanner sc=new Scanner(file);
sc.nextLine();
评论(5)

你需要使用class BufferedReader中的readLine()方法。 从该类中创建一个新的对象,对他操作这个方法并将其保存为一个字符串。

BufferReader Javadoc

评论(1)