Java ObjectOutput不会写对象,IO写异常

标签 java serialization

我在将序列化对象写入文件时遇到问题。我的保存例程总是在 ObjectOutput.writeObject(..) 处失败

我有一个 Inventory 类,其中包含项目的链接列表。 MainView 是程序的主 JFrame,它还保存 Inventory 对象。

我以前曾这样做过,但我决定重写我的程序的绝大多数。看来我做了一些事情把事情搞砸了,但我不知道是什么。

如果您需要更多代码,请告诉我,但我认为这涵盖了全部内容。

元素类别

import java.io.Serializable;

public class Item implements Serializable
{
    private static final long serialVersionUID = 1L;

    private String m_sItemNumber;

    public String getItemNumber(){return m_sItemNumber;}
    public void setItemNumber(String newVal){m_sItemNumber = newVal;}


    public void setDefaults()
    {
        m_sItemNumber = "notset";
    }

}

库存类别

import java.io.Serializable;
import java.util.LinkedList;

public class Inventory implements Serializable
{
    private static final long serialVersionUID = 1L;

    MainView mainView;
    LinkedList<Item> Inventory = new LinkedList<>(); 

    int indexOfCurrentItem;

    public void setMainView(MainView view)
    {
        mainView = view;
    }

    public boolean addItem(Item it)
    {
        Inventory.add(it);
        return true;
    }

    public Item getCurrentItem()
    {
        return Inventory.get(indexOfCurrentItem);
    }

    public Item getItemByIndex(int index)
    {
        return Inventory.get(index);
    }

}

IOHandler串口保存函数

public boolean saveSerialInv(String arg)
    {
        loadSaveDialog.newStatusLine("   Starting Inventory Save");

        ObjectOutput out = null;
        try 
        {
            out = new ObjectOutputStream(new FileOutputStream(arg));
        } 
        catch (FileNotFoundException e)
        {
            loadSaveDialog.newStatusLine("   Could Not Create File");
            return false;
        } 
        catch (IOException e) 
        {
            mainView.newStatusLine("   IO File save Exception");
            return false;
        }

        try 
        {
            out.writeObject(mainView.inventoryOfItems); // !! Fails here. !!
        } 
        catch (IOException e) 
        {
            loadSaveDialog.newStatusLine("   IO Write-- Exception");
            try 
            {
                    out.close();
        } 
            catch (IOException e1) 
            {
                    loadSaveDialog.newStatusLine("   IO Close save file Exception");
                    return false;
                }
            return false;
        }

        try 
        {
            out.close();
        } 
        catch (IOException e) 
        {
            loadSaveDialog.newStatusLine("   IO Close save file Exception");
            return false;
        }
        return true;
    }

主视图类

public class MainView extends javax.swing.JFrame 
{


    static LoadSaveDialog loadSave = new LoadSaveDialog();
    static ItemInfoDialog itemInfo = new ItemInfoDialog();
    Inventory inventoryOfItems = new Inventory();
    boolean invLoaded = false;

     public MainView() 
     {
        initComponents();
        loadSave.setMainView(this);
        inventoryOfItems.setMainView(this);
    }
}

最佳答案

序列化的一般规则是,类中的所有属性都应该是可序列化或标记为 transient 。没有关于 MainView 类是否可序列化的信息,如果不是,则将其标记为 transient 。

  MainView mainView;

无法从对象流中读取/写入任何不可序列化的属性。因此这将会失败:

        out.writeObject(mainView.inventoryOfItems); // !! Fails here. !!

关于Java ObjectOutput不会写对象,IO写异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18244612/

相关文章:

java - 如何在我的java项目中实现单点登录?

java - Gradle 与 Maven 中的依赖路径差异

java - 序列化树并存储到 RandomAccessFile

c# - 字典的 WCF 反序列化,其中枚举类型是关键

java - 如何将 bytearray 转换为 img 标签?

java - 在 Tapestry 5.3 中链接多个选择组件(Ajax 更新)

javascript - 如何保存带有循环引用的对象?

php - 如何序列化/反序列化 SimpleXML 对象?

c++ - boost 将 unordered_map 保存到磁盘的能力

java - 编写Java解释器插件