Java
Java
Java java.io.Serializable
Java
Serializable java.io.Serializable Java
import java.io.Serializable;
public class MyClass implements Serializable {
//
}
public class MyClass implements Serializable {
//
}
ObjectOutputStream
MyClass obj = new MyClass();
try {
FileOutputStream fileOut = new FileOutputStream("object.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(obj);
out.close();
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileOutputStream fileOut = new FileOutputStream("object.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(obj);
out.close();
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
"object.ser" obj
ObjectInputStream
MyClass obj = null;
try {
FileInputStream fileIn = new FileInputStream("object.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
obj = (MyClass) in.readObject();
in.close();
fileIn.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
FileInputStream fileIn = new FileInputStream("object.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
obj = (MyClass) in.readObject();
in.close();
fileIn.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
"object.ser" MyClass
ObjectInputStream ObjectOutputStream
ObjectOutputStream
public final void writeObject(Object x) throws IOException
ObjectInputStream
public final Object readObject() throws IOException,
ClassNotFoundException
Object
Java Employee Employee Serializable
Employee.java
public class Employee implements java.io.Serializable
{
public String name;
public String address;
public transient int SSN;
public int number;
public void mailCheck()
{
System.out.println("Mailing a check to " + name
+ " " + address);
}
}
java.io.Serializable
Java java.io.Serializable
ObjectOutputStream SerializeDemo Employee
employee.ser
Java .ser
SerializeDemo.java
import java.io.*;
public class SerializeDemo
{
public static void main(String [] args)
{
Employee e = new Employee();
e.name = "Reyan Ali";
e.address = "Phokka Kuan, Ambehta Peer";
e.SSN = 11122333;
e.number = 101;
try
{
FileOutputStream fileOut =
new FileOutputStream("/tmp/employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/employee.ser");
}catch(IOException i)
{
i.printStackTrace();
}
}
}
DeserializeDemo /tmp/employee.ser Employee
DeserializeDemo.java
import java.io.*;
public class DeserializeDemo
{
public static void main(String [] args)
{
Employee e = null;
try
{
FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
}
}
Deserialized Employee... Name: Reyan Ali Address:Phokka Kuan, Ambehta Peer SSN: 0 Number:101
readObject() try/catch ClassNotFoundException JVM JVM ClassNotFoundException
readObject() Employee
SSN 111222333 Employee SSN 0
193***[email protected]
Serializable
SerializableObjectOutputStream
private void writeObject0(Object obj, boolean unshared) throws IOException { ... if (obj instanceof String) { writeString((String) obj, unshared); } else if (cl.isArray()) { writeArray(obj, desc, unshared); } else if (obj instanceof Enum) { writeEnum((Enum) obj, desc, unshared); } else if (obj instanceof Serializable) { writeOrdinaryObject(obj, desc, unshared); } else { if (extendedDebugInfo) { throw new NotSerializableException(cl.getName() + "\n" + debugInfoStack.toString()); } else { throw new NotSerializableException(cl.getName()); } } ... }StringEnumSerializableNotSerializableException
193***[email protected]
hunter
hun***[email protected]
java
Serializable
transient
transient
hunter
hun***[email protected]
krion
a92***[email protected]
SerializableID
SerializableID ID Serializable id
serialVersionUID serialVersionUID serialVersionUID InvalidClassException
krion
a92***[email protected]
ByVie
635***[email protected]
ObjectOutputStream()
ObjectOutputStreamJava
ObjectOutputStream
ObjectOutputStream(OutputStream out)
ObjectOutputStream
void writeObject(Object obj)
ObjectOutputStream
tips: Serializable
public class Demo01ObjectOutputStream { public static void main(String[] args) throws IOException { //1. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day12\\file03-obj.txt")); //2. writeObject Person p = new Person("", 100); oos.writeObject(p); //3. oos.close(); } }ObjectInputStream()
ObjectInputStream Java
ObjectInputStream
ObjectInputStream(InputStream in)
ObjectInputStream
Object readObject()
tips readObject ClassNotFoundException
:
static
static transienttransient transient
public class Demo03StaticAndTransient { public static void main(String[] args) throws IOException, ClassNotFoundException { writePerson(); readPerson(); } //Person public static void readPerson() throws IOException, ClassNotFoundException { // ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day12\\file04-obj.txt")); // Object obj = ois.readObject(); System.out.println(obj); // ois.close(); } //Person public static void writePerson() throws IOException { // ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day12\\file04-obj.txt")); //Person oos.writeObject(new Person("", 100)); // oos.close(); } }public class Demo04SerialVersionUID { public static void main(String[] args) throws IOException, ClassNotFoundException { //writePerson(); readPerson(); } //Person public static void readPerson() throws IOException, ClassNotFoundException { // ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day12\\file04-obj.txt")); // Object obj = ois.readObject(); System.out.println(obj); // ois.close(); } //Person public static void writePerson() throws IOException { // ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day12\\file04-obj.txt")); //Person oos.writeObject(new Person("", 100)); // oos.close(); } }/* 1. Studentlist.txt 2. list.txt 1. Student 2. Student 3. ObjectOutputStream 4. writeObject 5. 6. ObjectInputStream 7. readObject 8. */ public class Demo05Test { public static void main(String[] args) throws IOException, ClassNotFoundException { //1. Student List<Student> list = new ArrayList<>(); //2. Student list.add(new Student("", 20)); list.add(new Student("", 22)); list.add(new Student("", 25)); //3. ObjectOutputStream ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day12\\list.txt")); //4. writeObject oos.writeObject(list); //5. oos.close(); //6. ObjectInputStream ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day12\\list.txt")); //7. readObject List<Student> list2 = (List<Student>) ois.readObject(); //8. for (Student stu : list2) { System.out.println(stu); } } }ByVie
635***[email protected]
156***[email protected]
1transient
2transienttransientSerializable
3transienttransient
156***[email protected]