我使用这是我从网上复制以下基本函数读取文本文件如何让Java使用Scanner读取非常大的文件?
public void read()
{
File file = new File("/Users/MAK/Desktop/data.txt");
System.out.println("Start");
try
{
//
// Create a new Scanner object which will read the data from the
// file passed in. To check if there are more line to read from it
// we check by calling the scanner.hasNextLine() method. We then
// read line one by one till all line is read.
//
Scanner scanner = new Scanner(file);
int lineCount = 0;
if (scanner == null)
{
System.out.println("Null File");
}
else
{
System.out.println(scanner.toString());
}
while (scanner.hasNextLine())
{
String line = scanner.nextLine();
System.out.println("Line " + lineCount +" contain : " + line);
lineCount++;
}
System.out.println("End of Try Bluck");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
System.out.println("Exception Bluck");
}
System.out.println("End");
}
}
它的正常工作与中小尺寸文件(其中包含10 20万余行的数据) 然而它无法使用包含50万行的文件。我没有收到错误(至少没有看到任何人)。那么发生了什么?我应该在这里做些什么来准备这么大的文件?
注意:我已经在运行带有4 GB RAM的Windows Server 2008的测试机器上增加了2 GB的堆。但这并没有多大帮助!
请任何人都可以告诉我我应该在这里做什么?
更新01
以下是输出
开始
java.util.Scanner中[定界符= \ p {javaWhitespace} +] [位置= 0 ] [[] = []] [[]] [[]] [[]] [[]] [ E] [正后缀=] [负后缀=] [NaN st环= \ Q \ E] [无穷大串= \Q∞\ E]的尝试Bluck
末
你是最棒的! :) BufferedReader解决了这个问题! 谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢! – 2010-02-11 09:01:52