Adam Chalkley wrote:. . . I wrote builder.append that didn't add to it?
I missed that part. Sorry for my mistake.
Don't know why you aren't appending the two Strings: print them out before appending.
How did you work out position? How do you know you are reading the right place in the file? What will happen if you try to read UTF from somewhere containing different sorts of data? If you return the byte, have you put anything into it first? Open the file with a hexadecimal editor and see whether you can find the names in the file.
Is this supposed to be part of the same assignment? Is that how you created that file.dat file?
In which case I shall merge the two threads together and they will be in both fora, principally in the IO forum.
I tried your code, cut down to this:-and opened the file with hexedit chalkley/file.dat and this is what it contains:-Count the bytes; there are 27 of them (or more precisely 0x1b of them since all those numbers are in hexadecimal). If each dot represents a non‑word character, you should be able to find the letters, particularly if you refer to this Unicode/ASCII table. You can also work out where they are. Remember what type of number you were writing and work out how many bytes they need in a file; remind yourself of the figures here.
Now, go back to the tutorials and see what it says about positions and how to find the next datum in such a file.
Also check the difference between the writeInt method and the write method.
Try putting an e.printStackTrace(); in there. I bet you're getting an EOF error. Our you putting the length of the string in the first two bytes?
Javadoc for readUTF() wrote:
Reads in a string from this file. The string has been encoded using a modified UTF-8 format.
The first two bytes are read, starting from the current file pointer, as if by readUnsignedShort. This value gives the number of following bytes that are in the encoded string, not the length of the resulting string. The following bytes are then interpreted as bytes encoding characters in the modified UTF-8 format and are converted into characters.
This method blocks until all the bytes are read, the end of the stream is detected, or an exception is thrown.
All things are lawful, but not all things are profitable.