public class SerializeToFlatFile { public static void main(String[] args) { SerializeToFlatFile ser = new SerializeToFlatFile(); ser.savePerson(); ser.restorePerson(); }
public void savePerson() { Person myPerson = new Person("Jay", 24); try { FileOutputStream fos = new FileOutputStream( "C:\\Documents and Settings\\l_huiqing\\デスクトップ\\技术调研\\serial\\myPerson.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); System.out.println("Person--Jay,24---Written"); System.out.println("Name is: " + myPerson.getName()); System.out.println("Age is: " + myPerson.getAge());
public void restorePerson() { try { FileInputStream fis = new FileInputStream( "C:\\Documents and Settings\\l_huiqing\\デスクトップ\\技术调研\\serial\\myPerson.txt"); ObjectInputStream ois = new ObjectInputStream(fis);
Person myPerson = (Person) ois.readObject(); System.out.println("\n--------------------\n"); System.out.println("Person--Jay,24---Restored"); System.out.println("Name is: " + myPerson.getName()); System.out.println("Age is: " + myPerson.getAge()); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }