import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class ObjectStreamDemo {
/**
* 没有方法的接口通常叫标记接口。
* @param args
* @throws IOException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
// TODO 自动生成的方法存根
writeobj();
readobj();
}
public static void writeobj() throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt"));
//写入一个对像
oos.writeObject(new person("wanghai",21,"ch"));
oos.close();
}
public static void readobj() throws IOException, ClassNotFoundException{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oos.txt"));
person p = (person)ois.readObject();
System.out.print(p);
ois.close();
}
}
class person implements Serializable{
public static final long serialVersionUID = 42L;
private String name;
int age;
static String country = "cn";
person(String name,int age,String country){
this.name = name;
this.age = age;
this.country = country;
}
public String toString(){
return name+":"+age+":"+country;
}
}
age和name的数据读存储在内存中,那么请问:如果想堆内存中的数据也不被序列化,那该怎么做?
还有country的数据"ch"能否写入到文件oos.txt中? |
|