Reputation: 9801
What is the absolute fastest way to read and write strings from a file with Java?
I need to read a file of known format into a String[]
— where each line is one item in the array — and then back to the file.
The reading, in particular, must be as fast as possible.
Is there a better way then just using a BufferedReader
and reading line by line into an array?
Upvotes: 1
Views: 946
Reputation: 827
Just a crazy idea: you could write the length of each string in the file. Something like:
BufferedInputStream stream=new BufferedInputStream(new FileInputStream("file.bin"));
byte[] buff=new byte[256];
String[] result=new String[10];
for(int i=0;i<10;i++){
int n=(reader.read()<<8)|reader.read(); // string length (assuming all strings are less than 64K)
if(buff.length<n) buff=new byte[n];
reader.read(buff,0,n);
result[i]=new String(buff,0,n);
}
stream.close();
This will free the BufferedReader
from checking every input byte for \n
. Though I'm not sure that this will be faster than readLine()
.
Upvotes: 1
Reputation: 2079
Here would be my first pass, assuming that memory is not an issue (ha).
Keep in mind that Java stores character data with UCS-16 internally, which means that your nice ASCII file is going to take x2 the size on disk to account for the "expansion." e.g. You have a 4,124 byte foo.txt file will be at least 8,248 bytes in memory.
Everything else is going to be slower, because the application will be designed to deal with some sort of buffering and wrapping (in particular, to deal with not having enough memory to deal with the file size).
Good luck!
Upvotes: 0