Large file reader

I participate in the Scanner class for training, and I use it to read a very large file (60,000 lines of aprox) without using the Reader class, and it stops reading after about 400 lines. Should I use Bufferedreader inside the Scanner constructor, or is the problem something else? I want to know why this is happening. Thank you My code is regular code to output all lines.

File file1 = new File("file1"); Scanner in= new Scanner(file1); while (scan.hasNextLine() ) { String str = scan.nextLine(); System.out.println(str); } 
+6
source share
2 answers

This problem is usually more common on 64-bit machines or with files larger than 1-2 GB and has nothing to do with a lot of space. Switch to BufferedReader, it should work fine,

 BufferedReader br = new BufferedReader(new FileReader(filepath)); String line = ""; while((line=br.readLine())!=null) { // do something } 
+4
source

I just experienced this problem. This seems to work just by redesigning the scanner. Replace this:

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

with this:

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

Perhaps the problem arises when you create a scanner from a file without a system, knowing that it is a text file.

+2
source

Source: https://habr.com/ru/post/957507/


All Articles