Match the file in memory!
Java 7 code:
FileChannel channel = FileChannel.open(Paths.get("/path/to/file"), StandardOpenOption.READ); ByteBuffer buf = channel.map(0, channel.size(), FileChannel.MapMode.READ_ONLY);
See here for more details.
If you are using Java 6, you need to:
RandomAccessFile file = new RandomAccessFile("/path/to/file", "r"); FileChannel channel = file.getChannel();
You can even use .asIntBuffer() in the buffer if you want. And you can only read what you really need to read when you need to read it. And it does not affect your heap.
source share