Simple use of java object stream, java object stream
Object input/output stream: used to write object information and read object information. Make the object persistent.
ObjectInputStream: Object input stream
ObjectOutPutStream: Object output stream
Simple instance
1 import java. io. file; 2 import java. io. fileInputStream; 3 import java. io. fileOutputStream; 4 import java. io. IOException; 5 import java. io. objectInputStream; 6 import java. io. objectOutputStream; 7 import java. io. serializable; 8 9 // create the class to be written to the disk. This class must implement the interface Serializable (Serializable) 10 class Student implements Serializable {11 12 // This ensures the uniqueness of serialVersionUID and prevents temporary changes to attribute variables, as a result, the write id is different from the read id 13 private static final long serialVersionUID = 1L; 14 int id; // an additional attribute 15 16 String name; 17 transient String sex needs to be added; // The transient modifier attribute, indicating that the attribute is temporary. this attribute will not be written to disk 18 transient int age; 19 20 public Student (String name, String sex, int age) {21 this. name = name; 22 this. sex = sex; 23 this. age = age; 24} 25} 26 27 28 public class objectIO {29 30/** 31 * @ param args32 * @ throws IOException 33 * @ throws ClassNotFoundException 34 */35 public static void main (String [] args) throws IOException, ClassNotFoundException {36 // TODO Auto-generated method stub37 38 createObj (); 39 readObj (); 40} 41 42 // (1) write 43 public static void createObj () throws IOException {44 // 1. create the target path 45 File file = new File ("C: \ Users \ bigerf \ Desktop \ objTest.txt"); 46 // 2. create channels 47 FileOutputStream fos = new FileOutputStream (file); 48 // 3. create an object output stream 49 ObjectOutputStream objOP = new ObjectOutputStream (fos); 50 // 4. create a Class Object and initialize 51 Student stu = new Student ("Mary", "male", 18); 52 // 5. write object 53 objOP to the target path file. writeObject (stu); 54 // 6. close resource 55 objOP. close (); 56} 57 58 // read the object 59 public static void readObj () throws IOException, ClassNotFoundException {60 File file = new File ("C: \ Users \ bigerf \ Desktop \ objTest.txt "); 61 FileInputStream FD = new FileInputStream (file); 62 ObjectInputStream objIP = new ObjectInputStream (FS ); 63 // to read object data, You Need To forcibly convert the object stream to the type of the object to be written 64 Student stu = (Student) objIP. readObject (); 65 System. out. println ("\ n name:" + stu. name + "\ n sex:" + stu. sex + "\ n age:" + stu. age); 66 objIP. close (); 67} 68 69}
Print Effect
Name: Mary Sue sex: null // The connected property following it uses the transient modifier age: 0
Usage: writeObject (Object o); // write an Object to the disk
ReadObject (); // read the disk object. Note that the type must be mandatory here.
Note:
1. If you want to write an object to a disk, the class to which the object belongs must be serialized to implement the Serializable interface. The Serializable interface does not have any method and is a mark interface.
2. If the member variable of the class to which the object belongs changes, you will report an error when reading the original object. If you want to solve the error, ensure that the serialVersionUID is unique.
3. If you do not want to save some information to the disk, you can modify the member variables with the transient keyword.
4. If another class is referenced in a class, the other class must also implement the Serializable interface.
If: