慕粉3170877
2016-07-25 16:05
package com.imooc;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class ObjectSeriaDemo {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
/*ObjectOutputStream oos=new ObjectOutputStream( new FileOutputStream("demo/obj1.dat"));
Foo2 foo2=new Foo2();
oos.writeObject(foo2);
oos.flush();
oos.close();
/* ObjectInputStream ois=new ObjectInputStream( new FileInputStream("demo/obj1.dat"));
Foo2 foo2=(Foo2)ois.readObject();
System.out.println(foo2);
ois.close();*/
ObjectOutputStream oos=new ObjectOutputStream( new FileOutputStream("demo/obj1.dat"));
Bar2 bar2=new Bar2();
oos.writeObject(bar2);
oos.flush();
oos.close();
/* ObjectInputStream ois=new ObjectInputStream( new FileInputStream("demo/obj1.dat"));
Bar2 bar2=(Bar2)ois.readObject();
System.out.println(bar2);
ois.close();*/
}
class Foo implements Serializable{
public Foo(){
System.out.println("foo");
}
}
class Foo1 extends Foo{
public Foo1(){
System.out.println("foo1");
}
}
class Foo2 extends Foo1{
public Foo2(){
System.out.println("foo2");
}
}
class Bar{
public Bar(){
System.out.println("bar");
}
}
class Bar1 extends Bar implements Serializable{
public Bar1(){
System.out.println("bar1");
}
}
class Bar2 extends Bar1{
public Bar2(){
System.out.println("bar2");
}
}
}
1、定义类 Foo、Foo1、Foo2 、 Bar、 Bar1、Bar2时要定义在类ObjectSeriaDemo的花括号外面 2、要运行起来的话,要在当前项目下新建个文件夹,文件夹的名字为demo package com.imooc; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectSeriaDemo { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub /* * ObjectOutputStream oos=new ObjectOutputStream( new * FileOutputStream("demo/obj1.dat")); Foo2 foo2=new Foo2(); * oos.writeObject(foo2); oos.flush(); oos.close(); /* ObjectInputStream * ois=new ObjectInputStream( new FileInputStream("demo/obj1.dat")); * Foo2 foo2=(Foo2)ois.readObject(); System.out.println(foo2); * ois.close(); */ ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream( "demo/obj1.dat")); Bar2 bar2 = new Bar2(); oos.writeObject(bar2); oos.flush(); oos.close(); /* * ObjectInputStream ois=new ObjectInputStream( new * FileInputStream("demo/obj1.dat")); Bar2 bar2=(Bar2)ois.readObject(); * System.out.println(bar2); ois.close(); */ } class Foo implements Serializable { public Foo() { System.out.println("foo"); } } class Foo1 extends Foo { public Foo1() { System.out.println("foo1"); } } class Foo2 extends Foo1 { public Foo2() { System.out.println("foo2"); } } class Bar { public Bar() { System.out.println("bar"); } } class Bar1 extends Bar implements Serializable { public Bar1() { System.out.println("bar1"); } } class Bar2 extends Bar1 { public Bar2() { System.out.println("bar2"); } } }
文件传输基础——Java IO流
133754 学习 · 1030 问题
相似问题