import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Person implements Serializable {
String name ;
int ssn ;
String phone ;
public Person(String name, int ssn, String phone) {
super();
this.name = name;
this.ssn = ssn;
this.phone = phone;
}
public String toString() {
return name + "/" + ssn + "/" + phone ;
}
public static void main(String[] args) {
Person p = new Person("Kathy" , 22334 , "010-234-5674") ;
ObjectOutputStream out = null ;
try {
out = new ObjectOutputStream(new FileOutputStream("person.data")) ;
out.writeObject(p);
System.out.println("Person:" + p + " was written") ;
} catch (IOException e) {
e.printStackTrace();
} finally {
try { out.close() ; } catch(Exception e) {}
}
Person p2 = null ;
ObjectInputStream in = null ;
try {
in = new ObjectInputStream(new FileInputStream("person.data")) ;
p2 = (Person) in.readObject() ;
System.out.println("Person:" + p2 + " was read") ;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally { try { in.close(); } catch(Exception e) {} }
}
}
댓글 없음:
댓글 쓰기