Scanner reading large file

前端 未结 2 762
囚心锁ツ
囚心锁ツ 2020-12-11 05:04

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 re

相关标签:
2条回答
  • 2020-12-11 05:28

    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
    }
    
    0 讨论(0)
  • 2020-12-11 05:37

    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.

    0 讨论(0)
提交回复
热议问题