?? 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);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -