标签:aac jni 输出 inf lpn 两种 read cto qpi
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);
personIn.setMoney(200);
// 通过在序列化之前,使用类变量直接赋值,可知
// 一个静态变量不管是否被transient修饰,均不能被序列化),反序列化后类中static型变量username的值为当前JVM中
// 对应static变量的值,这个值是JVM中的不是反序列化得出的
GamePerson.level = 300;
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());
System.out.println(personOut.getMoney());
}
}
class GamePerson implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
public static int level;
private int forceValue;
private int defenseValue;
// 金额是敏感数据:不对此属性进行序列化
private transient long money;
public long getMoney() {
return money;
}
public void setMoney(long money) {
this.money = money;
}
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;
}
}
import java.io.Externalizable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; /** * @descripiton Externalizable接口的使用 * * @author Alexia * @date 2013-10-15 * */ public class ExternalizableTest implements Externalizable { private transient String content = "是的,我将会被序列化,不管我是否被transient关键字修饰"; @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(content); } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { content = (String) in.readObject(); } public static void main(String[] args) throws Exception { ExternalizableTest et = new ExternalizableTest(); ObjectOutput out = new ObjectOutputStream(new FileOutputStream( new File("test"))); out.writeObject(et); ObjectInput in = new ObjectInputStream(new FileInputStream(new File( "test"))); et = (ExternalizableTest) in.readObject(); System.out.println(et.content); out.close(); in.close(); } }
标签:aac jni 输出 inf lpn 两种 read cto qpi
原文地址:https://www.cnblogs.com/lingyejun/p/9496854.html