标签:class cep puts java序列化 oid file read 等等 输出
public class Student implements Serializable { private String name; private char sex; public Student() { } public Student(String name,char sex) { this.name = name; this.sex = sex; } public void setName(String name) { this.name = name; } public void setSex(char sex) { this.sex = sex; } public String getName() { return this.name; } public char getSex() { return this.sex; } } public class UseStudent { public static void main(String[] args) { Student st = new Student("Tom",‘M‘); File file = new File("O:\\Java\\com\\jieke\\io\\student.txt"); try { file.createNewFile(); } catch(IOException e) { e.printStackTrace(); } try { //Student对象序列化过程 FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(st); oos.flush(); oos.close(); fos.close(); //Student对象反序列化过程 FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); Student st1 = (Student) ois.readObject(); System.out.println("name = " + st1.getName()); System.out.println("sex = " + st1.getSex()); ois.close(); fis.close(); } catch(ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
public class Person implements Externalizable { private static final long serialVersionUID = -842029427676826563L; public static String name; private int age; private transient int workDay = 5; private String fClub; public Person() { System.out.println("none-arg constructor"); } public Person(int age, String fClub) { this.age = age; this.fClub = fClub; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getWorkDay() { return workDay; } public void setWorkDay(int workDay) { this.workDay = workDay; } public String getfClub() { return fClub; } public void setfClub(String fClub) { this.fClub = fClub; } private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject();//运行默认的序列化机制 out.writeInt(workDay); System.out.println("正在进行序列持久化"); } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); workDay = in.readInt(); System.out.println("读取持久化对象"); } @Override public void readExternal(ObjectInput arg0) throws IOException, ClassNotFoundException { // TODO Auto-generated method stub } @Override public void writeExternal(ObjectOutput arg0) throws IOException { // TODO Auto-generated method stub } } public class Hello { public static void main(String[] args) { Person person = new Person(26, "Juventus"); person.setWorkDay(7); try { FileOutputStream fs = new FileOutputStream("foo.ser"); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject(person); os.close(); Person.name = "Alex"; FileInputStream in = new FileInputStream("foo.ser"); ObjectInputStream s = new ObjectInputStream(in); Person p = (Person) s.readObject(); System.out.println("name==" + Person.name + " age==" + p.getAge() + " workDay==" + p.getWorkDay() + " fClub==" + p.getfClub()); } catch (Exception e) { e.printStackTrace(); } } }
Java序列化Serializable和Externalizable
标签:class cep puts java序列化 oid file read 等等 输出
原文地址:http://www.cnblogs.com/slgkaifa/p/7029135.html