1

I have a randomaccess file that stores name, address, city, state, and zipcode.

They are all a fixed size of bytes in the file (each entry). How would I read from the file into a string for each item like name, street, etc?

I've tried

 string name = raf.readUTF(); 

But I can't control how many bytes it reads will that stop it from working correctly? Or how does readUTF work exactly since no amount of total bytes to read can be supplied in the arguement?

Or is a better approach to do

 ENTRY1_SIZE = 32;
 raf.read(string1);
 raf.skipBytes(ENTRY1_SIZE - string1.size());
 raf.read(string2);

and so on like that.

What would be the most efficient way to do this while still using a randomaccessfile? Basically I want to know how to read N bytes into a string that can be displayed in a textbox later.


Response to the answer below. I tried your method

        long position = 91;
        byte[] b = new byte[32];
        try{
        raf.seek(position);
        raf.readFully(b, position, 32);
        String name = (b, StandardCharsets.UTF_8);

But this error: The method readFully(byte[], int, int) in the type RandomAccessFile is not applicable for the arguments (byte[], long, int)

What should be supplied for the middle argument? The current file pointer is a long so that wouldn't work either.

Another error: StandardCharsets cannot be resolved or is not a field

Mkey
  • 155
  • 1
  • 4
  • 12
  • What character encoding do you use? How are strings stored which need fewer bytes than the fixed size? – vanje Nov 18 '15 at 13:29
  • Before proceeding, make sure you [understand what a character encoding is](http://stackoverflow.com/questions/10611455/what-is-character-encoding-and-why-should-i-bother-with-it). – Raedwald Nov 18 '15 at 13:53

1 Answers1

1

The javadoc has your answer. readUTF is meant to be used only for strings that have been previously written to the file using writeUTF. If your file has fixed-length regions, you should use readfully(byte[]) using an appropriately-sized byte array, and then convert the byte array to a string using new String(bytes, StandardCharsets.UTF_8).

Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70
  • readFully(byte[] b, int off, int len) would the offset just be raf.getFilePointer() ? – Mkey Nov 18 '15 at 13:32
  • @Mikey no, you first have to position the file pointer before calling any read methods. The offset argument in this overload of readFully is an offset into the byte array, for cases where you don't want to write at the beginning of the array. I've edited my answer to show explicitly that I was talking about readFully(byte[]). – Klitos Kyriacou Nov 18 '15 at 13:34
  • Gotcha. As for the String name = (b, StandardCharsets.UTF_8); that doesn't seem to want to work with me. Telling me standardCharsets.UTF_8 cannot be resolved to a value. – Mkey Nov 18 '15 at 13:38
  • Import java.nio.charset.StandardCharsets. – Klitos Kyriacou Nov 18 '15 at 13:42
  • Still not working after the import. So I was thinking I've padded each record in the file. Meaning if it's 32 bytes and only 20 bytes supplied I added 12 spaces to fill up 32 then wrote it to file using writeUTF. How would I get readUTF to read the exact amount of bytes needed? Or how does readUTF know when to stop reading? – Mkey Nov 18 '15 at 13:59
  • So I have done the readfully into a byte array but I can't figure out hwo to convert that to a string – Mkey Nov 18 '15 at 14:10
  • Hold on, are you writing to the file using writeUTF? In that case, read it with readUTF. Not readFully. As the previously-mentioned javadoc says, writeUTF first writes the length of the string in front of the actual data, so readUTF knows how long it is. – Klitos Kyriacou Nov 18 '15 at 15:44
  • Oh awesome thank you so much, I was confused on what they meant by an unsigned value represents the string size for writeUTF, I had no clue what determined how much of a string it read in at a time til now. – Mkey Nov 18 '15 at 16:52