Scanner reading large file

混江龙づ霸主 提交于 2019-11-28 12:17:46
Ankit Rustagi

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
}
beibichunai

I just experienced this very problem. It seems that it works just by changing the scanner construction. Replace this:

File file1 = new File("file1");
Scanner in= new Scanner(file1);

with this:

FileReader file1 = new FileReader("file1");
Scanner in= new Scanner(file1);

Maybe the problem appears when you build your scanner from a file without the system knowing that it is a text file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!