Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading huge line of string from text file

I have a large text file but doesn't have any line break. It just contains a long String (1 huge line of String with all ASCII characters), but so far anything works just fine as I can be able to read the whole line into memory in Java, but i am wondering if there could be a memory leak issue as the file becomes so big like 5GB+ and the program can't read the whole file into memory at once, so in that case what will be the best way to read such file ? Can we break the huge line into 2 parts or even multiple chunks ?

Here's how I read the file

   BufferedReader buf = new BufferedReader(new FileReader("input.txt"));
   String line;
   while((line = buf.readLine()) != null){

   }
like image 574
peter Avatar asked Jan 27 '26 03:01

peter


1 Answers

A single String can be only 2 billion characters long and will use 2 byte per character, so if you could read a 5 GB line it would use 10 GB of memory.

I suggest you read the text in blocks.

Reader reader = new FileReader("input.txt");
try {
    char[] chars = new char[8192];
    for(int len; (len = reader.read(chars)) > 0;) {
        // process chars.
    }
} finally {
    reader.close();
}

This will use about 16 KB regardless of the size of the file.

like image 98
Peter Lawrey Avatar answered Jan 28 '26 15:01

Peter Lawrey