Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.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 读取二进制文件时发生异常_Java_Ioexception - Fatal编程技术网

Java 读取二进制文件时发生异常

Java 读取二进制文件时发生异常,java,ioexception,Java,Ioexception,我得到了这个例外。是因为我没有抓住他们吗 java.io.EOFException at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2577) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315) at java.io.ObjectInputStream.readObject(ObjectInp

我得到了这个例外。是因为我没有抓住他们吗

 java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2577)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at ReadStudentRecord.readRecord(ReadStudentRecord.java:34)
at ReadStudentRecordTest.main(ReadStudentRecordTest.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
这是我的类,它读取二进制文件:

 import java.io.*;


public class ReadStudentRecord {

private ObjectInputStream input;

public void openFile() {

    try {
        input = new ObjectInputStream(new FileInputStream("student.dat"));

    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
} // end of open file method

public void readRecord() {

    Q4 student;

    System.out.println("Student Name\t\t Student average Mark");
    try {

        while (true) {


            student = (Q4) input.readObject();

            System.out.println(student.getStudentName() + "\t\t\t" + student.averageMark(student.getMark()));

                 }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

}// end of readRecord method

public void closeFile() {
    try // close file and exit
    {
        if (input != null)
            input.close();
        System.exit(0);
    } // end try
    catch (IOException ioException) {
        System.err.println("Error closing file.");
        System.exit(1);
    } // end catch
} // end method closeFile
} // end of class
它工作得很好,只是我得到了上面的异常,我不知道是什么导致了它。
我只是不知道我做错了什么。

您正在无限循环中从文件中读取对象

while (true) {
    student = (Q4) input.readObject();
        ...
}
最终,流到达了文件的末尾,但您仍然需要更多的对象,这就是引发异常的原因


一个可能的解决方案是捕获EOFEException,这意味着不再存在对象

try {
    while (true) {
        student = (Q4) input.readObject();
            ...
    }
}
catch (EOFException eof) {
    // Reached end of file. Do anything here, if you want.
    // Or else, just ignore the end of file, and proceed out of the block.
}
finally {
    // Some other stuff.
    // Do not forget to close the stream.
    input.close();
}

您正在无限循环中从文件读取对象

while (true) {
    student = (Q4) input.readObject();
        ...
}
最终,流到达了文件的末尾,但您仍然需要更多的对象,这就是引发异常的原因


一个可能的解决方案是捕获EOFEException,这意味着不再存在对象

try {
    while (true) {
        student = (Q4) input.readObject();
            ...
    }
}
catch (EOFException eof) {
    // Reached end of file. Do anything here, if you want.
    // Or else, just ignore the end of file, and proceed out of the block.
}
finally {
    // Some other stuff.
    // Do not forget to close the stream.
    input.close();
}

最好

替换

while (true)

或者,在
readRecord
中的
catch(IOException e)
之前添加此项:

} catch (EOFException e) {
  // EOF reached
}

最好

替换

while (true)

或者,在
readRecord
中的
catch(IOException e)
之前添加此项:

} catch (EOFException e) {
  // EOF reached
}

@afsantos您不能使用
!=null
因为readObject将抛出异常,所以在EOF上不返回null。我认为您考虑的是
BufferedReader readLine()
,它在EOF上返回
null
。OP使用的是
ObjectInputStream
,而不是
BufferedReader
@xagyg确实,你是对的。我已经更新了你的答案,并将更新我的,以反映替代方案,这是捕捉到的例外。哈哈,我刚刚提交了替代方案之前,我看到了你的评论-伟大的头脑为了防水,我会这样做<代码>最后{try{if(input!=null)input.close();}catch(Exception e){/*在尝试清理时忽略异常,我们已经完成了*/}@xagyg我通常也会执行第二个try块,但我认为这可能会让OP有点困惑,因为他似乎还不太适应这个。但这是一个伟大的建议@afsantos您不能使用
!=null
因为readObject将抛出异常,所以在EOF上不返回null。我认为您考虑的是
BufferedReader readLine()
,它在EOF上返回
null
。OP使用的是
ObjectInputStream
,而不是
BufferedReader
@xagyg确实,你是对的。我已经更新了你的答案,并将更新我的,以反映替代方案,这是捕捉到的例外。哈哈,我刚刚提交了替代方案之前,我看到了你的评论-伟大的头脑为了防水,我会这样做<代码>最后{try{if(input!=null)input.close();}catch(Exception e){/*在尝试清理时忽略异常,我们已经完成了*/}@xagyg我通常也会执行第二个try块,但我认为这可能会让OP有点困惑,因为他似乎还不太适应这个。但这是一个伟大的建议!关于在任何其他
IOException
之前捕获
eofeexception
,这一点提得很好!现在,当我将while(true)替换为while(input.available>0)时,它没有给我任何回报。没有异常错误,尽管很好地提到,在任何其他
IOException
之前捕获
eofeexception
!现在,当我将while(true)替换为while(input.available>0)时,它没有给我任何回报。不过,没有异常错误