Files
javaSE-0113/javaSE-day10/src/com/inmind/object_stream03/Demo11.java

30 lines
929 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.inmind.object_stream03;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
/*
序列化流对象(ObjectOutputStream)
构造方法:
ObjectOutputStream(OutputStream out) 创建一个写入指定的OutputStream的ObjectOutputStream。
常用方法:
void writeObject(Object obj) 将指定的对象写入ObjectOutputStream。
注意要进行序列化被序列化的对象类必须实现一个接口Serializable,否则会报NotSerializableException
*/
public class Demo11 {
public static void main(String[] args) throws IOException {
Student s = new Student("张三", 20);
//序列化将java对象保存到文件中
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student3.txt"));
oos.writeObject(s);
oos.close();
}
}