标签:
数据流:
DataInputStream,DataOutputStream。可以用于与计算机无关的格式读写java的基本数据类型以及String对象
对象流:
ObjectInputStream,ObjectOutputStream.
序列化:保存内存中对象的“全景图”。
为了实现对象序列化,对应的类必须实现Serializable接口。
Serializable接口中没有定义任何的方法,称之为“标记接口”。
如果加上了一个transient修饰符,该属性的值将不被序列化。
反序列化:使用ObjectInputStream类,将字节序列还原成对象的过程。
1 package com.lovo.day2; 2 3 import java.io.IOException; 4 import java.io.Serializable; 5 import java.util.Date; 6 7 public class Student implements Serializable { 8 9 private static final long serialVersionUID = 4282130869653374904L; 10 11 private String name; 12 private int age; 13 private boolean sex; 14 private double money; 15 private Date birth; 16 private Course course; 17 18 public Student(String name, int age, boolean sex, double money, Date birth, 19 Course course) { 20 super(); 21 this.name = name; 22 this.age = age; 23 this.sex = sex; 24 this.money = money; 25 this.birth = birth; 26 this.course = course; 27 } 28 29 public String getName() { 30 return name; 31 } 32 33 public void setName(String name) { 34 this.name = name; 35 } 36 37 public int getAge() { 38 return age; 39 } 40 41 public void setAge(int age) { 42 this.age = age; 43 } 44 45 public boolean isSex() { 46 return sex; 47 } 48 49 public void setSex(boolean sex) { 50 this.sex = sex; 51 } 52 53 public double getMoney() { 54 return money; 55 } 56 57 public void setMoney(double money) { 58 this.money = money; 59 } 60 61 public Date getBirth() { 62 return birth; 63 } 64 65 public void setBirth(Date birth) { 66 this.birth = birth; 67 } 68 69 public Course getCourse() { 70 return course; 71 } 72 73 public void setCourse(Course course) { 74 this.course = course; 75 } 76 77 @Override 78 public String toString() { 79 return "Student [name=" + name + ", age=" + age + ", sex=" + sex 80 + ", money=" + money + ", birth=" + birth + ", course=" 81 + course + "]"; 82 } 83 84 /* private void readObject(java.io.ObjectInputStream in) throws IOException, 85 ClassNotFoundException { 86 System.out.println("读"); 87 this.name = in.readUTF(); 88 this.age = in.readInt(); 89 this.sex = in.readBoolean(); 90 this.course = (Course) in.readObject(); 91 this.money = in.readDouble(); 92 } 93 94 private void writeObject(java.io.ObjectOutputStream out) throws IOException { 95 System.out.println("写"); 96 out.writeUTF(this.name); 97 out.writeInt(this.age); 98 out.writeBoolean(this.sex); 99 out.writeObject(course); 100 out.writeDouble(this.money); 101 } 102 */ 103 }
1 package com.lovo.day2; 2 3 import java.io.Serializable; 4 5 public class Course implements Serializable { 6 7 private static final long serialVersionUID = 1L; 8 9 private int code; 10 private String name; 11 12 public int getCode() { 13 return code; 14 } 15 16 public void setCode(int code) { 17 this.code = code; 18 } 19 20 public String getName() { 21 return name; 22 } 23 24 public void setName(String name) { 25 this.name = name; 26 } 27 28 public Course(int code, String name) { 29 super(); 30 this.code = code; 31 this.name = name; 32 } 33 34 @Override 35 public String toString() { 36 return "Course [code=" + code + ", name=" + name + "]"; 37 } 38 }
1 package com.lovo.day2; 2 3 import java.io.BufferedOutputStream; 4 import java.io.DataInputStream; 5 import java.io.DataOutputStream; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.util.Date; 11 12 public class DataTest { 13 14 public static void main(String[] args) { 15 Student stu = new Student("张飞", 18, true, 10.0, new Date(), new Course( 16 1, "语文")); 17 18 /* 写 */ 19 DataOutputStream out = null; 20 21 try { 22 // out = new DataOutputStream(new FileOutputStream("d:\\test.txt")); 23 out = new DataOutputStream(new BufferedOutputStream( 24 new FileOutputStream("d:\\test.txt"))); 25 26 out.writeUTF(stu.getName()); 27 out.writeInt(stu.getAge()); 28 out.writeBoolean(stu.isSex()); 29 out.writeDouble(stu.getMoney()); 30 } catch (FileNotFoundException e) { 31 e.printStackTrace(); 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } finally { 35 if (out != null) { 36 try { 37 out.close(); 38 } catch (IOException e) { 39 e.printStackTrace(); 40 } 41 } 42 } 43 44 /* 读 */ 45 DataInputStream in = null; 46 47 try { 48 in = new DataInputStream(new FileInputStream("d:\\test.txt")); 49 50 String name = in.readUTF(); 51 int age = in.readInt(); 52 boolean sex = in.readBoolean(); 53 double money = in.readDouble(); 54 55 System.out.println("name=" + name + ", age=" + age + ", sex=" + sex 56 + ", money=" + money); 57 } catch (FileNotFoundException e) { 58 e.printStackTrace(); 59 } catch (IOException e) { 60 e.printStackTrace(); 61 } finally { 62 if (in != null) 63 try { 64 in.close(); 65 } catch (IOException e) { 66 e.printStackTrace(); 67 } 68 } 69 } 70 }
标签:
原文地址:http://www.cnblogs.com/hellokitty1/p/4473952.html