【问题标题】:EOF exception for readObjectreadObject 的 EOF 异常
【发布时间】:2012-04-25 13:34:19
【问题描述】:

我想知道是否有办法检查ObjectInputStream 或ObjectOutputStream 是否为空。我的意思是,在我的程序中。第一次运行时,ObjectInputStream 将使用它的readObject() 方法,因为文件仍然是空的,它给了我一个EOF 异常(文件结尾)所以我想检查它是否为空摆脱异常:

我做得对吗?对于序列化,我在客户端和服务器中创建了具有相同名称和属性的类。

public class KeyAdr implements Serializable{

 String adr;
 String key;


}
....

    static FileInputStream fIn=null;
    static ObjectInputStream oIn=null;
    private static KeyAdr test=new KeyAdr();

....

           fIn= new FileInputStream("d:\\someFile.ser");
           oIn = new ObjectInputStream(fIn);
           test= (KeyAdr) oIn.readObject(); 

编辑:

 static  File serAdrKey=new File("d:\\someFile.ser");
     static    ObjectOutputStream oOut;
     static    FileOutputStream fOut;
     static  final   Pattern WebUrlPattern = Pattern.compile (WebUrlRegex);
     private static String WebUrlStr;
     static KeyAdr letsDoIt= new KeyAdr();

....

 public static void openStreams() throws IOException
        {
         fOut= new FileOutputStream(serAdrKey);
     oOut = new ObjectOutputStream(fOut);

        }


        @Override
public void    beforeWindowOpen(NavigationEvent event) 
        {

                     temp=event.getURL().toString();


  Matcher WebUrlMatcher = WebUrlPattern.matcher (temp);
    if (WebUrlMatcher.matches ())
    {
        int n = WebUrlMatcher.groupCount ();
      for (int i = 0; i <= n; ++i) {
    WebUrlStr = WebUrlMatcher.group (i);

}

                    letsDoIt.adr=WebUrlStr;    

                    try {
                    oOut.writeObject(letsDoIt);

                } catch (IOException ex) {
                    Logger.getLogger(Cobratest2.class.getName()).log(Level.SEVERE, null, ex);
                }


                   try {
                oOut.flush();


                   } catch (IOException ex) {
                Logger.getLogger(Cobratest2.class.getName()).log(Level.SEVERE, null, ex);
            }

编辑 2

fIn= new FileInputStream("d:\\someFile.ser");
PushbackInputStream input = new PushbackInputStream(fIn);
int c = input.read();
if(c != -1)
{
  input.unread(c);
  oIn = new ObjectInputStream(input);
  test = (KeyAdr) oIn.readObject();
  // ......
}

编辑 3:

Edit2 代码给了我以下堆栈跟踪异常:

Exception in thread "main" java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2552)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
        at test.Test.processClient(Test.java:117)
            at test.Test.run(Test.java:92)
            at test.Test.main(Test.java:159)

【问题讨论】:

  • 你在哪里初始化了 ObjectOuputStream?
  • @Phani 我将它添加到问题中。我忘了写在这里
  • 查看我对您的第二次编辑的编辑。
  • @EJP 我的edit3怎么样?
  • 对不起,我一直在带你走花园小路。您需要首先构造PushbackInputStream,测试它的第一个read(),将结果推回,等等,然后构造ObjectInputStream。构造一个 ObjectInputStream 会读取,所以你不想先这样做。

标签: java serialization eof objectoutputstream


【解决方案1】:

我想检查它是否为空然后摆脱异常:

为什么?这就是 EOFException 的作用。

【讨论】:

  • @lonesome 您无法修复代码以消除异常。当您到达文件末尾时,它总是会发生,您无法确定何时会发生。
【解决方案2】:

您可能想先使用read 方法检查是否至少有1 个字节。

read() 保证在到达流末尾时返回 -1,如果第一个 read() 返回 -1,则文件必须为空

附录: 使用 FileInputStream 您应该能够标记/重置以避免丢失读取的字节。

但是,您可能希望事先使用 java.util.File 来检查文件是否为空。

【讨论】:

  • 这将绝对不起作用。它会丢弃序列化需要的数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-27
相关资源
最近更新 更多