文章目录
一、序列化和反序列化
java提供了一种对象序列化的机制。用一个字节序列可以表示一个对象,该字节序列包含该对象的数据、对象的类型、对象中存储的属性
等信息。字节序列化写出到文件之后,相当于文件中持久保存了一个对象的信息。
反之,该字节序列还可以从文件中读取回来,重构对象,对她进行反序列化。
需要注意的是:对象中不仅包含了字符,还包含了其他,所以使用字节流进行序列化。当对象被序列化之后,存储在硬盘的文件中的内容是二进制字节,我们无法看得懂的,所以需要看懂,java机制也提供了反序列化,可将内容进行反序列,通过Object接收该对象,又可以重新使用该对象。
1、ObjectOutputStream 类
继承了字节输出流,可用父类方法
常用构造方法
public ObjectOutputStream(OutputStream out) throws IOException
创建一个写入指定的OutputStream的ObjectOutputStream。
特有的成员方法
public final void writeObject(Object obj) throws IOException
将指定对象写入到ObjectOutputStream中
序列化操作 Serializable接口
一个对象要想被序列化,必须满足两个要求:
- 该类必须实现
java.io.Serializable
接口,Serializable
是一个标记接口(因为该接口只是作为一个可序列化或反序列化的一个通行证,会自动生产需要被序列化的这个类生产一个UID,所以只是一个标记,因此是标记型接口),不实现此接口的类将不会是任何状态序列化或反序列化,会派出NotSerializableException
- 该类的所有属性必须是可序列化的。如果有一个属性不需要可序列化的,则该属性必须著名是瞬态的,使用
transient
关键字修饰。
public class Student implements Serializable {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
public class ObjectSerializableTest {
public static void main(String[] args) throws IOException {
outputObject();
}
private static void outputObject() throws IOException {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("student.txt"));
objectOutputStream.writeObject(new Student("heroC",18));
objectOutputStream.flush();
objectOutputStream.close();
}
}
2、ObjectInputStream 类
常用构造方法
public ObjectInputStream(InputStream in) throws IOException
创建一个写入指定的InputStream的ObjectInputStream。
特有的成员方法
public final Object readObject() throws IOException,ClassNotFoundException
将指定对象读取到到ObjectInputStream中
public class ObjectSerializableTest {
public static void main(String[] args) throws IOException,ClassNotFoundException {
inputObject();
}
private static void inputObject() throws IOException, ClassNotFoundException {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("student.txt"));
Object object = objectInputStream.readObject();
//Student student = (Student)object;
System.out.println(object); // 对对象的toString方法
objectInputStream.close();
}
}
3、InvalidException 序列号冲突异常
在一个类实现序列化接口之后,会自动生成一个该类的serialVersionUID(序列化号),在序列化时,会将序列号一同序列化,在反序列时,会匹配文件中和该类的class文件中的序列号是否相同,如果相同就反序列化成功,如果不相同则抛出InvalidException
异常。当该类只要更改就会重新生成一个新的序列号。
不给定的话因为不同的JVM之间的序列化算法是不一样的,不同的JDK也可能不一样,不利于程序的移植,可能会让反序列化失败。
解决方法是,自己给定该类一个不变的序列号,这样被序列化之后,无论怎么更改该类,反序列都会成功。通过在可序列化类中声明为“serialVersionUID”的字段(该字段必须是静态的、最终的long类型字段)显示声明。
private static final long serialVersionUID = 1L;
// 序列化UID字段,可以是任意值
4、transient与static
transient 修饰符
transient修饰符是瞬态的意思,被修饰的属性不能被序列化,只是标识这个属性不需要序列化。如果不需要该属性被序列化,推荐使用transient修饰符
static 修饰符
被static修饰的属性,是与对象同时间创建的,是静态的,是会被其他对象使用的。通过该static修饰符修饰的属性,也不能被序列化。
个人总结:被序列化的属性基本上都是动态的。
练习:序列化集合
将对象保存到集合中,然后依次将其序列化。
public class SerialCollectionTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
serialCollection();
}
private static void serialCollection() throws IOException,ClassNotFoundException {
// 将对象保存到集合中
ArrayList<Student> arrayList = new ArrayList<>();
arrayList.add(new Student("heroC",18));
arrayList.add(new Student("yikeX",20));
// 将该集合序列化
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("serialCollection.txt"));
objectOutputStream.writeObject(arrayList);
objectOutputStream.flush();
// 读取集合反序列化
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("serialCollection.txt"));
Object object = objectInputStream.readObject();
ArrayList<Student> studentArrayList = (ArrayList<Student>)object;
for(Student student : studentArrayList){
System.out.println(student);
}
}
}
二、打印流
平时我们在控制台打印输出,是调用print
方法和println
方法完成的,这两个方法都来自于java.io.PrintStream
类,该类能够方便地打印各种数据类型的值,是一种便捷的输出方式。
1、PrintStream 类
printstream类继承了outputstream
System.out.println();
该句就是通过System类的静态常量PrintStream类型的out变量来调用println方法
public final static PrintStream out = null;
特点:
- 只负责数据的输出,不负责数据的读取
- 与其他输出流不同,PrintStream 永远不会抛出 IOException
- 有特有的方法print、println方法,可以输出任意类型的值
注意:
- 如果使用继承自父类的write方法写数据,那么在查看该数据的时候会自动查询编码,根据编码显示对应的数据信息;如写入97,那么最终会显示a
- 如果使用print、println方法写数据,那么写的数据会原样输出;如97,那么最终就会显示97
常用构造方法
-
public PrintStream(String fileName) throws FileNotFoundException
使用指定的文件名创建一个新的打印流。输出的目的地 -
public PrintStream(File file) throws FileNotFoundException
-
public PrintStream(OutputStream out)
public class PrintStreamTest {
public static void main(String[] args) throws FileNotFoundException {
printTest();
}
public static void printTest() throws FileNotFoundException {
PrintStream printStream = new PrintStream("printStream.txt");
printStream.write(97); // 在printStream.txt输出a
printStream.println(97); // 在printStream.txt输出97
printStream.close();
}
}
2、更改System.out.println()的输出目的
public class PrintStreamTest {
public static void main(String[] args) throws FileNotFoundException {
setSystemOutTarget();
}
public static void setSystemOutTarget() throws FileNotFoundException {
PrintStream printStream = new PrintStream("printStream.txt");
System.out.println("不巧,被输出到控制台了"); // 在控制台中输出
System.setOut(printStream); // 将out的输出目的更改了
System.out.println("heroC"); // 在printStream.txt输出
printStream.close();
}
}