Java对象流的使用

为了让对象持久化(把对象存储到本地),可以使用java的对象流处理对象,把对象的内容写到本地存储的文件中,也可以从本地文件中读取出来。也就是常说的序列化和反序列化

主要用到了ObjectInputStream(对象输入流)   ObjectOutPutStream(对象输出流 )处理对象

使用对象流处理对象进行持久化的顺序是,先创建目标路径,然后创建流通道,之后调用对象流

以下代码声明了两个类分别是Student,Teacher,都有三个属性,在创建对象的时候赋值

代码:

package demo_io;

import java.io.*;

public class Test {
public static void main(String[] args) { Student s = new Student(1001, "lisi", "男");
Teacher t = new Teacher(1, "s", "girl");
print(s,t);
out();
} public static void print(Student s,Teacher t) {
//声明一个文件(创建文件)
File file = null;
//声明文件输出字节流
FileOutputStream fos = null;
//声明对象处理流
ObjectOutputStream oos = null;
try {
file = new File("E:\\a.txt");
fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
//向文件中写入对象的数据
oos.writeObject(s);
oos.writeObject(t);
//清空缓冲区
oos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//关闭资源
fos.close();
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} public static void out() {
File file = null;
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
file = new File("E:\\a.txt");
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
Student s = (Student) ois.readObject();
Teacher t = (Teacher) ois.readObject();
System.out.println(s.toString());
System.out.println(t.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
fis.close();
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
//创建一个可以序列化的类,必须实现Serializable接口,接口中没有任何方法,只是一个标记
class Student implements Serializable{
public int id;
public String name;
public String sex; public Student(int id, String name, String sex) {
this.id = id;
this.name = name;
this.sex = sex;
} @Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
//创建一个可以序列化的类,必须实现Serializable接口,接口中没有任何方法,只是一个标记
class Teacher implements Serializable{
public int id;
//声明一个暂时的属性,用transient标记的属性不被序列化,不能被写入磁盘
public transient String name;
public String sex; public Teacher(int id, String name, String sex) {
this.id = id;
this.name = name;
this.sex = sex;
} @Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
writeObject(Object o);  //向磁盘写入对象

readObject();  //读取磁盘的对象,注意这里需要强制类型

以下是在本地的文件中的数据(出现乱码),它不是简单的把对象属性值写入IO流中,而是按照一定的数据格式写入的。而这种格式,不是记事本、写字板、Word等文本编辑器能够识别的,因为,这些数据,压根就不是文本数据。只有使用相同版本的Java的ObjectInputStream来进行读取操作。

sr demo_io.Student聇鋏Knb I idL namet Ljava/lang/String;L sexq ~ xp  閠 lisit 鐢穝r demo_io.Teacher?椮嗲Wm I idL nameq ~ L sexq ~ xp   t st girl

实际输出:因为Teacher中的name用transient修饰的属性,所以不能被保存

Student{id=1001, name='lisi', sex='男'}
Teacher{id=1, name='null', sex='girl'}

在使用流的时候,注意要清空缓冲区,还有就是最后要关闭流,如果你不想将某些信息存入到磁盘 就可以同过transient关键字修饰成员变量,想要序列化必须要实现Serializable接口,这个接口中没有任何方法,只是标记告诉虚拟机这个类要序列化。

上一篇:Opencv step by step - 阈值化


下一篇:android MultiDex multidex原理原理下遇见的N个深坑(二)