【问题标题】:Reading/Writing objects to file returning null读取/写入对象到文件返回 null
【发布时间】:2019-08-29 04:31:54
【问题描述】:

我正在尝试读取对象并将其写入文件。将输出读入一个新对象是可行的,但每个值都是空的。

代码如下:

public void read() throws Exception
    {
        try
        {
            FileInputStream fIn = new FileInputStream(file);
            ObjectInputStream in = new ObjectInputStream(fIn);

            Object obj = in.readObject();

            System.out.println(obj);
public void save() throws Exception
    {
        FileOutputStream fOut = new FileOutputStream(file.toString());
        ObjectOutputStream out = new ObjectOutputStream(fOut);

        out.writeObject(this);
        out.flush();
        out.close();
    }

这是文件输出:(image of output)

我想在创建的新对象中接收我之前写入文件的值,但是我得到的所有值都是 null。

编辑:由于人们要求整个班级,而我不知道是什么代码可能导致什么,这是整个 UserFile 班级:https://pastebin.com/Gr1tcGsg

【问题讨论】:

  • 是正确类的新对象吗?还要显示您已序列化的类的来源。你可能有错误。
  • NB 你不需要file.toString()。只需file 即可。您也不需要在关闭前冲洗。
  • @user207421 close 调用flush,是的,他在关闭之前正在flush。
  • 好的,我读到他没有冲洗。作为对象流的一种良好做法,在写入推送标头后刷新它很好。但这是特定流实现的一般规则。
  • 不,他们不是。这是 ObjectOutputStream 的一般规则。

标签: java objectinputstream objectoutputstream


【解决方案1】:

我已经运行了该代码并且它有效,这意味着您很可能在编写之前就阅读了,或者您遇到了诸如 InvalidClassException: no valid constructor 之类的异常,这对您的情况是有意义的。

我运行的代码:

public class SavedObject implements Serializable
{
    public static void main(String[] args) throws IOException, ClassNotFoundException
    {
        new SavedObject();
    }

    private final int random;

    private SavedObject() throws IOException, ClassNotFoundException
    {
        random = ThreadLocalRandom.current().nextInt();
        File file = new File("Object.txt");
        save(file);
        read(file);
    }

    private void save(File file) throws IOException
    {
        FileOutputStream fileOutput = new FileOutputStream(file);
        ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);

        objectOutput.writeObject(this);
        objectOutput.close();
        fileOutput.close();

        System.out.println(this);
    }

    private void read(File file) throws IOException, ClassNotFoundException
    {
        FileInputStream fileInput = new FileInputStream(file);
        ObjectInputStream objectInput = new ObjectInputStream(fileInput);

        Object obj = objectInput.readObject();

        System.out.println(obj);

        objectInput.close();
        fileInput.close();
    }

    public String toString()
    {
        return "SavedObject(Random: " + random + ")";
    }
}

哪些打印:

SavedObject(Random: -2145716528)
SavedObject(Random: -2145716528)

还有一些提示:

  • 如果您throws,请不要尝试捕获
  • 拥有更易读的变量名
  • 在下一个问题中发送更多代码
  • 不推荐使用ObjectOutputStream,如果可以的话,你应该按原样写值
  • 不要使用throws“异常”,而是使用throws
  • 放入文件而不是file.toString()

【讨论】:

    【解决方案2】:

    我发现了问题,因为在我的构造函数中我没有正确应用从文件中检索到的信息。感谢大家的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      • 2020-03-16
      • 2023-03-23
      相关资源
      最近更新 更多