📄 serializationdemo2.java
字号:
//定制串行化实例
import java.io.*;
class Student implements Serializable{
String name;
String sex;
int age;
String birthday;
public Student(String name,String sex,int age,String birthday){
this.name = name;
this.sex = sex;
this.age = age;
this.birthday = birthday;
}
private void writeObject(ObjectOutputStream outObj) throws IOException
{
outObj.writeUTF(name);
outObj.writeUTF(sex);
outObj.writeInt(age);
outObj.writeUTF(birthday);
}
private void readObject(ObjectInputStream inObj) throws IOException
{
name = inObj.readUTF();
sex = inObj.readUTF();
age = inObj.readInt();
birthday = inObj.readUTF();
}
}
public class SerializationDemo2{
public static void main(String args[]) throws IOException , ClassNotFoundException
{
Student StudentObj = new Student("zhang li","female", 21 , "1985-12-23");
FileOutputStream fileOStream = new FileOutputStream("test11_9.txt");
ObjectOutputStream objOutStream = new ObjectOutputStream(fileOStream);
try
{
objOutStream .writeObject(StudentObj);
objOutStream .close();
}
catch (IOException e )
{
System.out.println(e);
}
StudentObj = null;
FileInputStream fileInStream = new FileInputStream("test11_9.txt");
ObjectInputStream objInStream = new ObjectInputStream(fileInStream);
try
{
StudentObj = (Student)objInStream.readObject();
objInStream.close();
}
catch (IOException e )
{
System.out.println(e );
}
System.out.println("**********************************");
System.out.println("There are information about Student:");
System.out.println("Student Name:"+StudentObj.name);
System.out.println("Student sex :"+StudentObj.sex);
System.out.println("Student age:"+StudentObj.age);
System.out.println("Student birthday:"+StudentObj.birthday);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -