I have this large file with the follow format:
Unique String
\t
The way I am going to suggest is to read the file, and keep track of the position. Store the position along the way in a map so you can look it up later.
The first way to do this is to use your file as a DataInput
, and use the RandomAccessFile#readline
RandomAccessFile raf = new RandomAccessFile("filename.txt", "r");
Map<String, Long> index = new HashMap<>();
Now, how is your data stored? If it is stored line by line, and the ecoding conforms to the DataInput
standards, then you can use.
lond start = raf.getFilePointer();
String line = raf.readLine();
String key = extractKeyFromLine(line);
index.put(key, start);
Now anytime you need to go back and get the data.
long position = index.get(key);
raf.seek(position);
String line = raf.readLine();
There are many caveats to this. For example, if you need a more robust encoding, then the first time you read it you'll want to create a reader that can manage the encoding, and just use your RandomAccessFile
as an input stream. readLine()
can fail if the lines are too large. Then you would have to devise your own strategy for extracting the key/data pair.