I want to efficiently read specific lines of a file in Java.
As of now, I have been using the method public int read(byte[] b)
from the class Random access file. Its fast, but is there any other technique which is much faster.
The file has around 200,000+ lines. I have collected offsets and length of each line. I use this in read(byte)
method. I usually have to read around 1-75000 specific lines. It takes more than 50s for this. Is it possible to optimise this?
RandomAccessFile dicti = new RandomAccessFile(file,"r");
for(int i:totalLines){
Long start = getOffset(ID);
dicti.seek(start);
byte[] bytes = new byte[lengthinBytesfortheLine(i)];
dicti.read(bytes);
line =new String(bytes);}