Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java writeInt(整数)覆盖零_Java_File_Randomaccessfile - Fatal编程技术网

Java writeInt(整数)覆盖零

Java writeInt(整数)覆盖零,java,file,randomaccessfile,Java,File,Randomaccessfile,使用RandomAccessFile类,我试图测试在java中写入/读取文件的概念。所以我尝试了以下代码: public static void main (String[] args) throws IOException { RandomAccessFile storage = new RandomAccessFile("FILE.txt", "rw"); storage.seek(0); storage.writeInt(1);

使用
RandomAccessFile
类,我试图测试在java中写入/读取文件的概念。所以我尝试了以下代码:

public static void main (String[] args) throws IOException {
        RandomAccessFile storage = new RandomAccessFile("FILE.txt", "rw");

        storage.seek(0);
        storage.writeInt(1);

        storage.seek(1);
        storage.writeInt(2);

        storage.seek(2);
        storage.writeInt(3);

        storage.seek(3);
        storage.writeInt(4);

        storage.seek(4);
        storage.writeInt(5);

        System.out.println(storage.readInt());
        System.out.println(storage.readInt());
        System.out.println(storage.readInt());
        System.out.println(storage.readInt());
        System.out.println(storage.readInt());

        storage.close();
我认为应该打印: 1. 2. 3. 4. 五,

但实际情况是,它会打印: 3. 4. 5. EOFEException。。。为什么?

我在这里看到两个问题:

  • 您没有在上次写入和首次读取之间进行
    seek
    -ing,因此您是在上次写入结束后开始读取。(您没有立即获得
    EOFEException
    这一事实表明您的文件在此程序运行开始时不是空的。)
  • seek
    接受一个表示字节位置的参数,但是
    writeInt
    readInt
    写入和读取四个字节。因此,如果您不希望整数彼此重叠,则需要搜索位置0、4、8等,而不是0、1、2等(不过,如果您的目标是使整数相邻,则实际上不需要当前调用
    seek

  • 这里有两个问题-您不允许每写入
    int
    4个字节,并且在将
    int
    s读回内存时,您没有返回到文件的开头

    首先,
    seek
    方法将字节数的参数作为偏移量输入文件

    storage.seek(0);
    
    System.out.println(storage.readInt());
    System.out.println(storage.readInt());
    System.out.println(storage.readInt());
    System.out.println(storage.readInt());
    System.out.println(storage.readInt());
    
    pos
    -从文件开头开始以字节为单位测量的偏移位置,用于设置文件指针

    但是在Java中,ints有4个字节,因此每次后续写入都会覆盖前面的int的4个字节中的3个。每次都将标记显式设置为4字节:

    storage.seek(4);
    storage.writeInt(2);
    
    storage.seek(8);
    storage.writeInt(3);
    
    // etc.
    
    更简单的是,标记“做正确的事情”并向前移动适当的字节数。只需在此处省略
    seek
    s

    storage.writeInt(1);
    storage.writeInt(2);
    storage.writeInt(3);
    storage.writeInt(4);
    storage.writeInt(5);
    
    第二个问题是,在回读字节时,没有将标记重置回文件的开头,从而导致出现
    EOFException
    。添加对
    seek(0)
    的调用,以将标记发送回文件的开头

    storage.seek(0);
    
    System.out.println(storage.readInt());
    System.out.println(storage.readInt());
    System.out.println(storage.readInt());
    System.out.println(storage.readInt());
    System.out.println(storage.readInt());
    
    然后我得到输出:

    1
    2
    3
    4
    5
    

    writeInt
    写入4个字节,但
    seek
    的参数仅以字节为单位,因此您已经覆盖了大量数据。你为什么打电话给seek?只需删除对
    seek
    的调用,就可以了,只是在开始阅读之前应该调用
    storage.seek(0)