package four.day;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class objectwriteDemo {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Reader();
//Writer();
}
public static void Writer() throws FileNotFoundException, IOException
{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("g:/java/java基础4/1.txt"));
oos.writeObject(new Person("lisi",30));
oos.close();
}
public static void Reader() throws FileNotFoundException, IOException, Exception
{
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("g:/java/java基础4/1.txt"));
//Person p=(Person)ois.readObject();
//System.out.println(p);
Object o=ois.readObject();
System.out.println(o);
ois.close();
}
}
-----------------------------------------------------------------person-----------------------------------------
package four.day;
import java.io.Serializable;
public class Person implements Serializable {
String name;
int age;
Person(String name,int age)
{
this.name=name;
this.age=age;
}
}
为什么用Person 和Object会分别读出不一样的结果?
//Person p=(Person)ois.readObject();
//System.out.println(p);//结果:four.day.Person@bf2d5e
Object o=ois.readObject();
System.out.println(o)//结果:four.day.Person@13bad12
|
|