package io;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.Serializable;
public class ObjectPrintIO {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("F://eclipseWorkStation//temp//to//沙耶之歌.txt");
// PrintStream ps = new PrintStream(fos);
// ps.println();
// ps.print("print");
// ps.print(5);
//Object
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(new Student());
oos.flush();
oos.close();
FileInputStream fis = new FileInputStream("F://eclipseWorkStation//temp//to//沙耶之歌1.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Student s =(Student)ois.readObject();
System.out.println(s.id);
ois.close();
fis.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
//Object 写入时必须序列化。
class Student implements Serializable{
int id = 1;
String name = "Mu";
}
读取Object时必须对号入座,txt在复制内容时会缺失一些符号。