Java世纪网 » JAVA技术整理板块 » Java 基础应用 » JDK » JAVA Serialize 的写入和读取
package test; import java.io.*; public class TestSerialize { public static void main(String[] args) { try { write(); read(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public static void write() { try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("d:/a.txt"))); student s = new student(); s.setAddr("河北石家庄"); s.setName("joejoe1991"); out.writeObject(s); s = new student(); s.setAddr("北京"); s.setName("joejoe2008"); out.writeObject(s); s = new student(); s.setAddr("广东"); s.setName("joejoe11"); out.writeObject(s); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } public static void read() { try { FileInputStream stream = new FileInputStream(new File("d:/a.txt")); ObjectInputStream in = new ObjectInputStream(stream); while (stream.available() > 0) { student s = (student) in.readObject(); System.out.println(s.getAddr()); System.out.println(s.getName()); System.out.println("********************"); } stream.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } } } class student implements Serializable { private static final long serialVersionUID = 1L; private String name; private String addr; public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
系统管理员
管理员
查看此人帖子
kimi
铜牌会员
老紫竹