关于java对象的反序列化
答案:3 悬赏:10 手机版
解决时间 2021-04-13 23:54
- 提问者网友:别再叽里呱啦
- 2021-04-13 13:47
利用反射和文件输入流把类的信息打印到文本文件中,如何实现从这个文件中获取类的信息?
最佳答案
- 五星知识达人网友:归鹤鸣
- 2021-04-13 15:13
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
public class $ {
public static void main(String[] args) {
Bean bean = new Bean("abcde", 20);
String path = "D:/a.txt";
try {
// 写入文件
OutputStream os = new FileOutputStream(path);
ObjectOutput ow = new ObjectOutputStream(os);
ow.writeObject(bean);
// 读取
InputStream is = new FileInputStream(path);
ObjectInput oi = new ObjectInputStream(is);
Bean bean2 = (Bean) oi.readObject();
System.out.println(bean2.getName());
System.out.println(bean2.getAge());
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Bean implements Serializable {
private String name;
private int age;
public Bean(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
public class $ {
public static void main(String[] args) {
Bean bean = new Bean("abcde", 20);
String path = "D:/a.txt";
try {
// 写入文件
OutputStream os = new FileOutputStream(path);
ObjectOutput ow = new ObjectOutputStream(os);
ow.writeObject(bean);
// 读取
InputStream is = new FileInputStream(path);
ObjectInput oi = new ObjectInputStream(is);
Bean bean2 = (Bean) oi.readObject();
System.out.println(bean2.getName());
System.out.println(bean2.getAge());
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Bean implements Serializable {
private String name;
private int age;
public Bean(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
全部回答
- 1楼网友:狂恋
- 2021-04-13 17:34
import java.io.serializable;
public class person implements serializable {
private static final long serialversionuid = -2071565876962058344l;
private string name;
// private int age;
private transient int age;
// int age;
public person() {
super();
}
public person(string name, int age) {
super();
this.name = name;
this.age = age;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public int getage() {
return age;
}
public void setage(int age) {
this.age = age;
}
@override
public string tostring() {
return "person [name=" + name + ", age=" + age + "]";
}
}
- 2楼网友:神鬼未生
- 2021-04-13 16:11
new ObjectInputStream(this.callback_data.getBinaryStream()).readObject(); 反序列化后,利用反射就可以得到里面的属性和方法了呀
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯