IO Serial Number AC

Person

 1 package MyTest;
 2 
 3 import java.io.Serializable;
 4 
 5 public class Person implements Serializable {
 6     /**
 7      * 
 8      */
 9     private static final long serialVersionUID = 1L;
10     private String name;
11     private int age;
12 
13     public Person(String name, int age) {
14         super();
15         this.name = name;
16         this.age = age;
17     }
18 
19     @Override
20     public String toString() {
21         return name + "\t" + age;
22     }
23 }
View Code

FileOutputStream

 1 package MyTest;
 2 
 3 import java.io.File;
 4 import java.io.FileOutputStream;
 5 import java.io.ObjectOutputStream;
 6 
 7 
 8 public class Out {
 9     public static void main(String[] args) throws Exception{
10         File file = new File("1.txt");
11         FileOutputStream fos = new FileOutputStream(file,true);
12         ObjectOutputStream oos = new ObjectOutputStream(fos);
13         oos.writeObject(new Person("qwe",18));
14         oos.writeObject(new Person("abc",20));
15         oos.close();
16     }
17 }
View Code

ObjectInputStream

 1 package MyTest;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.ObjectInputStream;
 6 
 7 public class In {
 8     public static void main(String[] args) throws Exception {
 9         File file = new File("1.txt");
10         FileInputStream fis = new FileInputStream(file);
11         ObjectInputStream ois = new ObjectInputStream(fis);
12         while(fis.available()>0) {
13             System.out.println(ois.readObject());            
14         }
15         ois.close();
16     }
17 }
View Code

Before true no additional information into txt

After additional

 

Description of the problem: using readObject java.io.ObjectInputStream class () method with the time to read the sequence of a plurality (two and more) class file, when reading the second class, will throw exceptions mentioned in the title.

The reason: Any file has a file header (header) and file body (body), java when additional way to write a file, every time he would append a header to the file, the header is unrecognized, so throw back the abnormality

Solution:

    Java objects provide an output stream can not solve the problem, we can write your own subclass java.io.ObjectOutputStream class that subclasses as follows:

 1 package MyTest;
 2 
 3 import java.io.IOException;
 4 import java.io.ObjectOutputStream;
 5 import java.io.OutputStream;
 6 
 7 public class MyObjectOutputStream extends ObjectOutputStream {
 8     protected MyObjectOutputStream() throws IOException, SecurityException {
 9         super();
10     }
11 
12     @Override
13     protected void writeStreamHeader() throws IOException {
14         super.writeStreamHeader();
15     }
16 
17     public MyObjectOutputStream(OutputStream out) throws IOException {
18         super(out);
19     }
20 }
View Code

This class overrides writeStreamHeader parent class () method for writing header information here so that he does not carry out any operation, using all the other parent class

In the logic code, the file to be written is determined whether the input to the first time, as follows:

 

Guess you like

Origin www.cnblogs.com/hellsino/p/11546168.html