1.java对象流介绍
对象流主要是将java中具体的对象转换为字节序列(配合文件流可以将对象保存在文件中)
也可以将序列化的对象反序列化转换为对象(配合文件流可以将文件中保存的对象读出来)
2.流操作对象的限制
public class Student implements Serializable{
对象必须实现Serializable这个空接口
3.ObjectInput与ObjectOutput对文件操作实例
public class ObjectTest { public static void main(String[] args) throws IOException, ClassNotFoundException { FileOutputStream fos=new FileOutputStream("c:\\jiangwei.data"); ObjectOutputStream os=new ObjectOutputStream(fos); os.writeObject(new Student(1,"xiaojiang",19)); os.writeObject(new Student(2,"xiaoming",18)); os.close(); fos.close(); FileInputStream fis=new FileInputStream("c:\\jiangwei.data"); ObjectInputStream ois=new ObjectInputStream(fis); Student st1=(Student)ois.readObject(); Student st2=(Student)ois.readObject(); fis.close(); ois.close(); System.out.println(st1.toString()); System.out.println(st2.toString()); } } ObjectOutputStream类构造方法ObjectOutputStream()和ObjectOutputStream(OutStream on)
ObjectInputStream类构造方法ObjectInputStream()和ObjectInputStream(InputStream in)