简体   繁体   中英

RandomAccessFile reading wrong Integers

So i'm using a RandomAccessFile to write data into a file. Since i'm compressing the written data with lz4 first write the uncompressed size as an integer in the file, then the compressed size as integer and finally the compressed byte array. However, when reading the two integers something completely different comes out. I wrote a simple test and these are the results:

    int test = 982364567;

    System.out.println("Writing " + test + " in file...");

    this.clusterData.seek(1);
    this.clusterData.write(test);

    this.clusterData.seek(1);
    System.out.println("Reading " + this.clusterData.readInt() + " from file...");

clusterData is the RandomAccessFile and the output in the console is this:

Writing 982364567 in file...
Reading -1761607680 from file...

Could someone explain to me what is going wrong and what i'm missing there?

Be ware that write(int data) means write a byte represented by data. Therefore it will write a single byte.

You need to use the writeInt(int data) in order to write the full integer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM