-
序列化:把对象以流的方式写入到文件中
反序列化: -
ObjectOutputStream 对象的序列化流
构造方法:ObjectOutputStream(OutputStream out) ; 创建写入指定的OutputStream 的ObjectOutputStream
特有的成员方法:void writeObject(object obj); 将指定的对象写入ObjectOutputStream
序列化和反序列化的时候,会抛出NotSerializableException没有序列化异常。类通过实现java.io.Seerializable接口以启用其序列化功能。未实现此接口的类将无法使用任何状态序列化或反序列化。
Serializable接口也叫标记型接口,要进性序列化和反序列化的类必须实现Serializable接口,就会给类添加一个标记。
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("oos.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
// 将一个员工对象,写出到文件中
Emp e = new Emp("zhangsan", 23);
e.salary = 3000.0;
e.password = "abcd1234";
oos.writeObject(e);
oos.close();
}
class Emp implements Serializable {
//自定义类,需要提供这个常量:当前类的序列化版本编号
private static final long serialVersionUID = 0x6666666666666666L;
private String name;
private int age;
//新添加属性
public double salary;
public transient String password;
// transient关键字的作用是,表示当前属性是临时的/瞬态的
// 序列化时,不包括这个瞬态的属性值
// 对于一些敏感的信息、或无用的信息,可标记为不用序列化!
public Emp(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Emp [name=" + name + ", age=" + age + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
-
ObjectInputStream 对象的反序列化流
把文件中保存的对象,以流的方式读取处来使用。
构造方法:ObjectInputStream(InputStream in);
特有的成员方法:Object readObject() 从 ObjectInputStream读取对象readObject方法声明抛出了ClassNotFoundException(class文件找不到异常)
当不存在的对象的class文件时抛出异常
反序列化的前提:(1)类必须实现Serializable
(2)必须存在类对应的class文件
public static void main(String[] args) throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream("oos.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
System.out.println(obj);
obj = ois.readObject();
System.out.println(obj);
Emp e = (Emp)obj;
System.out.println("员工的工资是:" + e.salary);
System.out.println("员工的密码是:" + e.password);
ois.close();
}
- transient关键字:瞬态关键字
被transient修饰的成员变量,不能被序列花
private transient int age;
oss.writeObject(new Person("小美",18);
object o = ois.readObject();
Person{name = "小美", age = 0};
static关键字:静态关键字
被static修饰的成员变量不能被序列化,序列化的都是对象
private transient int age;
oss.writeObject(new Person("小美",18);
object o = ois.readObject();
Person{name = "小美", age = 0};
- 序列化集合
public static void main(String[] args) throws IOException, ClassNotFoundException {
FileOutputStream fos = new FileOutputStream("list.dat");//节点流
BufferedOutputStream bos = new BufferedOutputStream(fos);//处理流
ObjectOutputStream oos = new ObjectOutputStream(bos);//处理流
List<Student> list = new ArrayList<Student>();
list.add(new Student("zhangsan", '男', 18, "001"));
list.add(new Student("lisi", '女', 19, "002"));
list.add(new Student("wangwu", '男', 20, "003"));
list.add(new Student("zhaoliu", '女', 21, "004"));
list.add(new Student("qianqi", '男', 22, "005"));
System.out.println("原先的集合:" + list);
oos.writeObject(list);
oos.close();
//将集合反序列化
FileInputStream fis = new FileInputStream("list.dat");
BufferedInputStream bis = new BufferedInputStream(fis);
ObjectInputStream ois = new ObjectInputStream(bis);
Object obj = ois.readObject();
System.out.println("反序列化得到的集合" + obj);
bis.close();
}
- printStream 打印流
为其他输出流添加量功能,使他们能够方便地打印各种数据值表示形式。
特点:(1)只负责数据的输出,不负责数据的读取。
(2)与其他输出流不用,printStream永远不会抛出IOException
(3)有特有的方法:void print(任意类型的值)
void println(任意类型的值并换行)
注意:(1)如果使用继承父类的write方法写数据,那么查看数据的时候会查询编码表
(2)如果使用自己的特有的方法print/println方法写数据,写的数据原样输出
public static void main(String[] args) throws FileNotFoundException {
PrintStream ps = new PrintStream("dot.dat");
//使用继承父类的write方法写数据,那么查看数据的时候会查询编码表98-》b
ps.write(98);
//使用自己特有的方法print/println方法写数据,写的数据原样输出
ps.println(98);
ps.println(9.8);
ps.println("hello");
//释放资源
ps.close();
}
可以改变输出语句的目的地(打印流的流向)
输出语句,默认在控制台输出
使用System.setOut方法改变输出语句的目的地改为参数中传递的打印流的目的地
static void setOut(PrintStream out)
重新分配“标准”输出流
public static void main(String[] args) throws FileNotFoundException {
System.out.println("我在控制台输出");
PrintStream ps = new PrintStream("11\\目的地是打印流.txt");
System.out.println(ps);//把输出语句的目的地改变为打印流的目的地
System.out.println("我在打印流的目的地中输出");
//释放资源
ps.close();
}