存储在文件中的序列化对象不可读 [英] Serialzed Objects Stored in File are not readable

查看:35
本文介绍了存储在文件中的序列化对象不可读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是,当我将序列化对象存储在 .txt 文件中时,它不是可读形式,并且包含一些随机符号和字母.首先我想知道这背后的原因是什么,然后如何解决这个问题.

The problem is that when I store Serialized Object in a .txt file it's not in readable form and contain some random symbols and letters. First of all I would like to know that what's the reason behind that and then how to solve this problem.

好的,这是我的代码:我想要序列化的对象

package serialized;

import java.io.Serializable;
import javax.swing.JOptionPane;

public class Student implements Serializable {

private static final long serialVersionUID = 1L;
String s_name;
int roll_no;

public Student(String s_name, int roll_no){
    this.s_name=s_name;
    this.roll_no=roll_no;
}

public void getInfo(){
    JOptionPane.showMessageDialog(null, "Name: "+s_name+" Roll No: " + roll_no);
}
}

序列化

package serialized;
import java.io.*;
public class StudentWriter {

public static void main(String[] args) {
    Student s1=new Student("Asiya", 58);
    try{

        FileOutputStream fos=new FileOutputStream("abc.txt");
        ObjectOutputStream oos=new ObjectOutputStream(fos);

        // Writing Object Info to File
        oos.writeObject(s1);
        System.out.println("Data has been written to file");
        oos.close();
        fos.close();

    }catch(Exception e){
        e.printStackTrace();
    }

}

}

反序列化

package serialized;
import java.io.*;
public class StudentReader {

public static void main(String[] args) {

    try{
        FileInputStream fis=new FileInputStream("abc.txt");
        ObjectInputStream ois=new ObjectInputStream(fis);

        // Reading Data from File
        Student s_data=(Student)ois.readObject();
        System.out.println("Data has been read");
        s_data.getInfo();

        ois.close();
        fis.close();

    }catch(Exception e){
        e.printStackTrace();
    }

}

}

输出:

 ’ sr serialized.Student        I roll_noL s_namet Ljava/lang/String;xp   :t      Asiya

推荐答案

存储在文件中的序列化对象不可读

Serialized Objects Stored in File are not readable

除了通过反序列化之外,它们并不意味着可读.

They aren't meant to be readable, other than via de-serialization.

问题是,当我将序列化对象存储在 .txt 文件中时,它不是可读形式,并且包含一些随机符号和字母.

The problem is that when I store Serialized Object in a .txt file it's not in readable form and contain some random symbols and letters.

那不是问题.唯一的问题是您错误地期望它应该是人类可读的.任何地方都没有这样的规范.

That's not a problem. The only problem is your misplaced expectation that it should be human-readable. There is no specification anywhere that says so.

首先我想知道这背后的原因是什么

First of all I would like to know that what's the reason behind that

它在对象序列化流协议.

然后如何解决这个问题.

and then how to solve this problem.

什么问题?您的代码工作正常.StudentReader 弹出的对话框中的输出是正确的:

What problem? Your code works correctly. The output in the dialog box put up by StudentReader is correct:

Name: Asiya Roll No: 58

您在此处清楚所做的是使用一些实用程序直接查看序列化文件,而不是运行您的 StudentReader 程序.

What you have clearly done here is look directly into the serialized file with some utility program, rather than run your StudentReader program.

这里没有问题需要解决.

There is no problem here to solve.

注意:

  1. 关闭对象流将关闭底层文件流.您无需自己关闭文件流.
  2. 序列化对象不是文本,不应保存在 .txt 文件中.

这篇关于存储在文件中的序列化对象不可读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆