- ObjectInputStream (byte stream) and ObjectOutputStream (byte stream)
- In addition to saving the basic data type, there are also custom objects, which are read by Object and need to be coerced to use.
- Serialization: Output stream,
- Deserialization: Input stream
- Consistent with data flow, it must be written before read, read and write in the same order.
- Serialized objects must implement the java.io.Serializable interface
-
Transit: This data does not need serialization, data transparency (output shows null),private transient String name;
// Read after writing. The number of read is consistent with that of write.
public class n { public static void main(String[]args) throws IOException, ClassNotFoundException {
Write byte array
// Write out serialization
ByteArrayOutputStream os=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(os));
ha ff=new ha("hh",222);oos.writeUTF("Ha-ha"); oos.writeChar('q'); oos.writeBoolean(false); oos.writeObject(ff); oos.flush(); byte[] datas=os.toByteArray(); oos.close(); //Read deserialization ObjectInputStream ois =new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas))); String s=ois.readUTF(); char ch=ois.readChar(); boolean flag=ois.readBoolean(); Object str=ois.readObject(); //Object requires mandatory transformation //Restore (determine which class it is, and then force conversion): if (str instanceof String ) //String classes are converted directly into strings { String s1=(String)str; System.out.println(s1); } if(str instanceof Data) { Date d=(Date)str; System.out.println(d); } if(str instanceof ha) { ha fr=(ha)str; System.out.println(fr.getName()+fr.getSalary()); } ois.close(); } //javabean for encapsulating data class ha implements java.io.Serializable { private transient String name; //This data does not need serialization and is transparent. private double salary; public ha() { } public ha(String name,double salary) { this.name=name; this.salary=salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
Write to the file:
ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("D:/d/s"))); ha ff=new ha("hh",222); oos.writeUTF("what"); oos.writeObject(ff); oos.flush(); oos.close(); //When used, deserialize objects ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(new FileInputStream("D:/d/s"))); String s=ois.readUTF(); Object it=ois.readObject(); if(it instanceof ha) { ha q=(ha)it; System.out.println(q.getName()+q.getSalary()); } ois.close(); //javabean for encapsulating data class ha implements java.io.Serializable { private transient String name; //This data does not need serialization and is transparent. private double salary; public ha() { } public ha(String name,double salary) { this.name=name; this.salary=salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }