RandomAccessFile 读取错误的整数

RandomAccessFile reading wrong Integers

所以我正在使用 RandomAccessFile 将数据写入文件。 因为我用 lz4 压缩写入的数据,首先将未压缩的大小作为整数写入文件,然后将压缩的大小作为整数写入,最后写入压缩的字节数组。然而,当读取这两个整数时,会出现完全不同的结果。我写了一个简单的测试,结果如下:

    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 是 RandomAccessFile,控制台中的输出是这样的:

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

有人可以向我解释哪里出了问题以及我遗漏了什么吗?

请注意,write(int data) 意味着写一个 byte 由数据表示。因此它将写入一个字节。

您需要使用 writeInt(int data) 才能写入完整的整数。