java - 我用IDEA把对象序列化成文件,但是不管我设置什么格式,打开的文件都是乱码
问题描述
我用IDEA生成文件,对文件中的对象进行序列化,但是不管设置什么编码格式,最终的内容都是乱码在这里输入图片描述各种编码都试过了,最后还是胡言乱语
我尝试了许多常见的编码格式
public class Student implements Serializable{
private String a1;
private String a2;
public Student(){
}
public Student(String a1, String a2) {
this.a1 = a1;
this.a2 = a2;
}
public String getA1() {
return a1;
}
public void setA1(String a1) {
this.a1 = a1;
}
public String getA2() {
return a2;
}
public void setA2(String a2) {
this.a2 = a2;
}
}
public class ObjectSeria {
public static void main(String[] args) throws Exception{
File file = new File("demo.txt");
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(file)
);
Student student = new Student("a","b");
oos.writeObject(student);
oos.flush();
oos.close();
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream(file)
);
Student student1 = (Student)ois.readObject();
System.out.println(student1);
ois.close();
}
}
我希望打开的文件正确显示
解决方案
推荐阅读
- mysql - MySQL上root用户的认证字符串
- javascript - 将 Javascript 和 CSS 组合成 HTML 文件
- html - 如何使此弹出窗口响应不同的屏幕尺寸
- android - 如何在android中解析没有json对象标题的json数组?帮我
- python - 在 Python 中分配给 for 循环变量时的令人困惑的行为
- reporting-services - 计算 SSRS 分组值
- python - 在 Python 中使用尚未定义的变量作为 kwarg
- spring-amqp - 使用 Spring AMQP 注释驱动回复 (@SentTo) 后处理消息
- xamarin - 尝试绑定 Zendesk IOS 框架时未解析可以绑定的类型
- ruby-on-rails - 如何从数据库中仅获取事件管理 webapp 中尚未发生的那些字段(事件)