序列化生成的文件,我写了个类就能拿到对象。那这还有什么意义,没什么加密作用啊?
是我理解错了吗?
我的测试代码:
class Cat implements Serializable{
private String name;
public Cat(){
this.name = "new cat";
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name=name;
}
public static void main(String []args){
Cat cat = new Cat();
try{
//这就是要序列化文件的存储位置,是由自己控制的。
//FileOutputStream fos = new FileOutputStream("zmy/catDemo2.txt");
FileOutputStream fos = new FileOutputStream("zmy/catDemo2.bat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println("1>"+cat.getName());
cat.setName("zMy cat");
oos.writeObject(cat);
oos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public class TestReadSerializable implements Serializable{
public static void main(String []args){
try{
FileInputStream fis = new FileInputStream("zmy/catDemo2.bat");
ObjectInputStream ois = new ObjectInputStream(fis);
Cat cat = (Cat)ois.readObject();
System.out.println(cat.getName());
ois.close();
}catch(Exception e){
e.printStackTrace();
}
}
}