简体   繁体   English

RandomAccessFile读取错误的整数

[英]RandomAccessFile reading wrong Integers

So i'm using a RandomAccessFile to write data into a file. 所以我正在使用RandomAccessFile将数据写入文件。 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. 由于我正在使用lz4压缩写入的数据,因此首先将未压缩的大小写为整数,然后将压缩后的大小写为整数,最后将其压缩为字节数组。 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: clusterData是RandomAccessFile,控制台中的输出是这样的:

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. 注意, write(int data)意味着写入由数据表示的byte Therefore it will write a single byte. 因此它将写入一个字节。

You need to use the writeInt(int data) in order to write the full integer. 您需要使用writeInt(int data)来写入完整整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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