2017-08-12 58 views
-2

在做序列化时,我发现在解序列化过程中,当文件被读取直到在while循环中使用readObject()然后EOFException在java中定制序列化过程

所以我想出了这个解决方案,创建了一个实现Serializable接口的类EofIndicatorClass。在序列化期间写入EofIndicatorClass的实例EOF以在序列化过程期间指示EOF

在序列化过程中,如果oin.readObject()返回instanceof EofIndicatorClass那意味着它是EOF,则退出while循环并且不会抛出EOFException

所以下面就是基于这种办法现在请告知程序是这样的apprach是正确的或有任何其他更好的方法存在

/* 
* Class whose instance will be written at EOF during serialization 
* to indicate EOF during deSerialization process. 
*/ 
class EofIndicatorClass implements Serializable{} 

其他类

class Employee implements Serializable { 

    private static final long serialVersionUID = 1L; 
    private String name; 

    public Employee(String name) { 
      this.name = name; 
    } 

    @Override 
    public String toString() { 
      return "Employee [name=" + name + "]"; 
    } 

} 

现在系列化发生

public class SerializeEmployee { 

    public static void main(String[] args) { 

     Employee object1 = new Employee("amy"); 
     Employee object2 = new Employee("ankit"); 

     try { 
       OutputStream fout = new FileOutputStream("ser.txt"); 
       ObjectOutput oout = new ObjectOutputStream(fout); 

       System.out.println("Serialization process has started, " 
          + "serializing employee objects..."); 
       oout.writeObject(object1); 
       oout.writeObject(object2); 

       //write instance of EofIndicatorClass at EOF 
       oout.writeObject(new EofIndicatorClass()); 

       oout.close(); 
       System.out.println("Object Serialization completed."); 

     } catch (IOException ioe) { 
       ioe.printStackTrace(); 
     } 

    } 

} 

现在发生钝化

public class DeSerializeEmployee { 
    public static void main(String[] args) { 
     InputStream fin; 
     try { 
       fin = new FileInputStream("ser.txt"); 
       ObjectInput oin = new ObjectInputStream(fin); 

       System.out.println("DeSerialization process has started, " 
          + "displaying employee objects..."); 

       /* 
       *If oin.readObject() returns instanceof EofIndicatorClass that means 
       *it's EOF, exit while loop and EOFException will not be thrown. 
       */ 
      Object obj; 
      while(!((obj = oin.readObject()) instanceof EofIndicatorClass)){ 
        System.out.println(obj); 
      } 

       oin.close(); 

     } catch (EOFException e) { 
       System.out.println("File ended"); 
     } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
     } catch (IOException e) { 
       e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
     } 

     System.out.println("Object DeSerialization completed."); 

    } 
} 
+0

为什么不使用Externalizable接口。 Externalizable的基本用途是定制序列化过程。 https://www.tuturself.com/posts/view?menuId=58&postId=393 –

+0

@ArpanDas用它来解决流'问题'的末端*如何?* – EJP

回答

0

虽然做系列化我有什么发现是,在反序列化过程在使用readObject()while环路,则EOFException被读取,直到最终文件。

正确。

,所以我想出了这个解决方案

解决方案是什么?所有你需要的是catch (EOFException e)。通过引入另一个必须被序列化的类,您将阻止将该类用于任何其他目的,并且您没有解决“问题”,因为任何输入流都可能在任何时候被截断,原因完全超出了您的控制范围,所以你仍然必须妥善处理EOFException

请告知是这种做法是正确的

这既是不足,多余的。

或有任何其他更好的方法存在

是。按照设计者的意图抓住EOFException。 API已经设计好了。这里没有问题要解决。