扫描仪读取大文件 [英] Scanner reading large file
问题描述
我正在玩Scanner类用于学习目的,我用它来读取一个非常大的文件(60.000行aprox)而不使用Reader类,它在大约400行后停止读取。我是否必须在Scanner的构造函数中使用Bufferedreader,否则问题是什么?我想知道为什么会这样。谢谢。
我的代码是输出所有行的常用代码。
I am playing around with the Scanner class for learning purposes and i use it to read a very large file (60.000 lines aprox) without using the Reader class , and it stops reading after approximately 400 lines. Do i have to use a Bufferedreader inside the Scanner's constructor or the problem is something else? I want to know why this is happening. Thanks. My code is the usual code to output all the lines.
File file1 = new File("file1");
Scanner in= new Scanner(file1);
while (scan.hasNextLine() ) {
String str = scan.nextLine();
System.out.println(str);
}
推荐答案
这个问题通常比较常见在64位计算机上或使用大小超过1-2 GB的文件,并且与堆空间无关。切换到BufferedReader它应该可以正常工作,
This issue is usually more common on 64 bit machines or with files having size more than 1-2 GB and does not have anything to do with heap space. Switch to BufferedReader it should work fine,
BufferedReader br = new BufferedReader(new FileReader(filepath));
String line = "";
while((line=br.readLine())!=null)
{
// do something
}
这篇关于扫描仪读取大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!