I have a Random Access File filled with Strings (I know that they are not really Strings, although it will help me to explain the problem). What I want to do is to view a certain String, let's say String #4. While it would be simple for integers and generally primitive data types as they have a fixed byte length and I can read the right bytes by suming up all the previous bytes.
I have managed to solve this problem by giving all the String a fixed length of 16 chars, so if I have the word "dog", then this word in the RAF is "dog " (dog + 13 spaces) and the byte length was fixed too. Again, I could easily read the right value using the following method:
static String loadOne(int n) throws IOException {
raf = new RandomAccessFile(file, "rw");
raf.seek((n-1)*(fix+2));
String x = raf.readUTF();
return x;
}
Where n is the number of the value I want to read and fix is the number of chars (and bytes) of one String.
Everything seemed fine until I used an extra ASCII character - a polish letter - in one of the Strings, because it consists of 2 bytes. The char lenght was still the same - 16, but there were 17 bytes and the whole thing fell apart.
What can I do?