标签:qpi 博客 读取 lin level 攻击 force 消失 evel
package test; 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 SerializableTest { public static void main(String[] args) throws IOException, ClassNotFoundException { FileOutputStream fos = new FileOutputStream("game-person.info"); ObjectOutputStream oos = new ObjectOutputStream(fos); GamePerson personIn = new GamePerson(); personIn.setName("abcde"); personIn.setLevel(1); personIn.setForceValue(2); personIn.setDefenseValue(3); oos.writeObject(personIn); oos.flush(); oos.close(); FileInputStream fis = new FileInputStream("game-person.info"); ObjectInputStream ois = new ObjectInputStream(fis); GamePerson personOut = (GamePerson) ois.readObject(); System.out.println(personOut.getName()); System.out.println(personOut.getLevel()); System.out.println(personOut.getForceValue()); System.out.println(personOut.getDefenseValue()); } } class GamePerson implements Serializable { private static final long serialVersionUID = 1L; private String name; private int level; private int forceValue; private int defenseValue; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public int getForceValue() { return forceValue; } public void setForceValue(int forceValue) { this.forceValue = forceValue; } public int getDefenseValue() { return defenseValue; } public void setDefenseValue(int defenseValue) { this.defenseValue = defenseValue; } }
运行之后,在workspce目录下会生成game-person.info二进制文件,我们打开看看
public class SerializableTest { public static void main(String[] args) throws IOException, ClassNotFoundException { FileInputStream fis = new FileInputStream("game-person.info"); ObjectInputStream ois = new ObjectInputStream(fis); GamePerson personOut = (GamePerson) ois.readObject(); System.out.println(personOut.getName()); System.out.println(personOut.getLevel()); System.out.println(personOut.getForceValue()); System.out.println(personOut.getDefenseValue()); } }
一刀999,惊不惊喜,意不意外!
除了本地--内存,这种序列化的应用场景外。另外一种也就是第二种情况是:需要把Java对象通过网络进行传输的时候。因为数据只能够以二进制的形式在网络中进行传输,因此当把对象通过网络发送出去之前需要先序列化成二进制数据,在接收端读到二进制数据之后反序列化成Java对象。
标签:qpi 博客 读取 lin level 攻击 force 消失 evel
原文地址:https://www.cnblogs.com/lingyejun/p/9496420.html