Java 序列化反序列化文件
2012-03-15 16:59
330 查看
import java.io.*; @SuppressWarnings("serial") class Person implements Serializable { public Person(String name, String sex, int age, int height) { this.name = name; this.sex = sex; this.age = age; this.height = height; } public String toString() { return "|" + this.name + "|" + this.sex + "|" + this.age + "|" + this.height + "|"; } public String name; public String sex; public int age; public int height; } public class SerialTest { public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { Person p = new Person("Jim", "male", 28, 194); // 开始序列化 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream( new File("myTest.txt"))); oos.writeObject(p); // 反序列化 ObjectInputStream ois = new ObjectInputStream(new FileInputStream( new File("myTest.txt"))); Person p1 = (Person) ois.readObject(); System.out.println(p1.toString()); } }
相关文章推荐
- 文章标题 Java中io流的一些简单操作(包含文件复制,向硬盘中写入文本文件,以及io流高级应用序列化和反序列化)
- Java中对文件的序列化和反序列化
- Java 序列化与反序列化 —— 序列化为一般二进制格式文件
- Java序列化与反序列化,文件操作
- Java 对象直接写入文件 --序列化和反序列化
- java序列化,反序列化及反序列如何判断是否已读到文件末尾
- Java中List或者HashMap对象-序列化和反序列化并附读写文件操作
- XStream java对象和json文件进行序列化和反序列化
- java中的序列化与反序列化,还包括将多个对象序列化到一个文件中
- Java Json/xml 序列化和反序列化 工具:JsonTools 和 simpleframework 附带实例
- [java]序列化和反序列化
- JAVA序列化和反序列化考点
- Java基础学习总结——Java对象的序列化和反序列化
- Java对象的serialVersionUID在序列化和反序列化的用途
- Java自定义序列化反序列化问题
- java对象的序列化和反序列化
- java简单序列化和反序列化
- Java 序列化以及反序列化
- java序列化(Serializable)的作用和反序列化
- Java对象的序列化和反序列化