Out of memory exception
You're running out of memory because you're trying to read too much of the file into memory. This could be happening in two ways I can think of.
You're doing it deliberately
If you're trying to save every line that you read in, you're going to run out or memory.
while ((curentLine = br.readLine()) != null) {
stringBuilder.append(currentLine);
}
If you're just trying to save 1000 lines at a time, you might be able to just increase Java's heap size with -Xmx
and be OK. It all depends on how much memory 1000 lines takes up.
You're doing it accidentally
If the file you're reading doesn't have any line breaks, then br.readLine()
will attempt to read the whole thing, believing that it's one gigantic long line.
Reading without going line-by-line
If you imagine an arbitrary file of text, it's just a long string of characters. Some of these characters (EOL
) have special meaning to humans and many programs, but they're still just characters. This means that you can't just say "give me the 10th line of text" without reading every character that comes before it (because you never know which character might be an EOL
that you need to count).
You could use a fixed-length record format: you say that each line will be exactly $n$ characters lone (80, say). Now if you want to jump to the 10th line, you can jump to the 800th character. But if you're actually using UTF-16, then characters aren't a char
and this doesn't really work.
That's OK, because you probably should be using a database at this point.