怎么破解序列化文件?
1).创建FileOutputStream(创建存取文件的FileOutputStream对象,如果文件不存在,它会自动被创建出来)
FileOutputStream fileStream=new FileOutputStream("MyGame.ser");
(2).创建ObjectOutputStream(它能让你写入对象,但无法直接地连接文件,所以需要参数的指引)
ObjectOutputStream os=new ObjectOutputStream(fileStream);
(3).写入对象(将变量所引用的对象序列化并写入MyGame.ser这个文件)
os.writeObject(characterOne);
os.writeObject(characterTwo);
os.writeObject(characterThree);
(4).关闭ObjectOutputStream(关闭所关联的输出串流)
os.close();
即:Object 写入 ObjectOutputStream 连接到 FileOutputStream 到文件
3.解序列化的步骤:还原对象
(1).创建FileInputStream(如果文件不存在就会抛出异常)
FileInputStream fileStream=new FileInputStream("MyGame.ser");
(2).创建ObjectInputStream(它知道如何读取对象,但是要链接的stream提供文件存取)
ObjectInputStream os=new ObjectInputStream(fileStream);
(3).读取对象(每次调用readObject方法都会从stream中读出下一个对象,读取顺序与写入顺序相同,次数超过会抛出异常)
Object one=os.readObject();
Object two=os.readObject();
Object three=os.readObject();
(4).转换对象类型(返回值是Object类型,因此必须转换类型)
GameCharacter elf=(GameCharacter)one;
GameCharacter troll=(GameCharacter)two;
GameCharacter magician=(GameCharacter)three;
(5).关闭ObjectInputStream(FileInputStream会自动跟着关掉)
os.close();
即:文件 被读取 FileInputStream 被连接 ObjectInputSteam 恢复成对象
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有