class Person implements Serializable{ public String name; public int age; Person(String name,int age){ this.name = name; this.age = age; }}public class Main { public static void main(String []args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { FileOutputStream out =new FileOutputStream("person.txt"); ObjectOutputStream obj_out = new ObjectOutputStream(out); obj_out.writeObject(new Person("z3",12)); }}
public interface Serializable {}
class Person implements Serializable{ public String name; public int age; Person(String name,int age){ this.name = name; this.age = age; }}public class Main { public static void main(String []args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { FileInputStream in =new FileInputStream("person.txt"); ObjectInput obj_in = new ObjectInputStream(in); Person p = (Person) obj_in.readObject(); System.out.println(p.name); }}
private Constructor<?> cons;
readSerialData(obj, desc);
Unsafe unsafe = Unsafe.getUnsafe();Person p = new Person("z3",12);unsafe.putObject(p, 16, "z5"); // 至于第二个参数为什么是16,不知道,调试时发现java就这么调的,可能16是"name"的一个标志?System.out.println(p.getName());