Question

I am trying to read and parse a few pieces of information from a file that will be stored in sequential order. ,a char[] of size 8 ,an int ,an int[] of size 8 and finally ,an int. So, I am reading 56 bytes of information. I am using RandomAccessFile and was wondering if I needed to seek() after preforming each operation of readChar() and readInt() or if I can just call these methods one after the other. I guess this is more of a question about whether the file pointer will reset after each operation completes or if it's safe so assume that the fp will follows it's last location until the file is closed.

Here is what I've written:

        int currentOffset = 128;
        for(int i = 0; i<16; i++){
            //Initialize nodes. 
            readDisk.seek(currentOffset);
            char[] name = new char[8];
            for(int j = 0; j<8; j++){
                name[i] = readDisk.readChar();
            }
            int size = readDisk.readInt();
            int[] blockPointers = new int[8];
            for(int j = 0; j<8; j++){
                blockPointers[i] = readDisk.readInt();
            }
            int used = readDisk.readInt();

Will the fp be at 156 after these operations? Thank you! Sorry if this is a silly question.

Was it helpful?

Solution

Yes, read moves file pointer by number of bytes read, try this

    RandomAccessFile f = new RandomAccessFile("1.txt", "r");
    f.readChar();
    System.out.println(f.getFilePointer());

output

2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top