Java – Reads a long string into memory

Reads a long string into memory… here is a solution to the problem.

Reads a long string into memory

I

have a very large string and when I read it in Java, I get an out of memory error. Actually, I need to read all these strings into memory and then split them into separate strings and sort them based on the values. What’s the best way to do it?

Thanks

Solution

Where do your large strings come from? As you said, I think it comes from the file. Do you have to know the whole string to know where to split it? If not, you can read the file character by character until you encounter a split mark, put all the characters read so far into one string, and start reading the next string. Can you roughly know where to sort the single string you just read? If so, you can write part of the string to a separate file on the first run (for example, when you sort strings alphabetically, all strings starting with A go to A.tmp). After that, you can sort the created file contents (hopefully small enough now to fit in your memory) and finally append the contents to the new output file.

Related Problems and Solutions