Java序列化 - readbObject有效,但对读取对象的操作不起作用

时间:2016-11-07 01:16:08

标签: java serialization

所以我创建了一个程序,可以创建/存储/操作测试和调查。我序列化创建,保存,加载和利用它们的对象。

到目前为止,序列化的所有内容都有效,但是当我尝试加载测试/调查对象时,我不能打印出与测试/调查相关的问题和可能的答案。

下面是FileLoader类中的代码,它负责保存和加载文档:

//--------------------------------------------------------------------------
/// Method : saveDocument
/// @Description - saves test to a file
/// @param[in] <test> - a test object to save
/// @return <void>
//--------------------------------------------------------------------------
public void saveDocument(AbstractQuestionContainer document)
{
    container = document;

    try
    {
        ObjectOutputStream objectOutput = new ObjectOutputStream(file);
        objectOutput.writeObject(container);

        objectOutput.close();           
    }
    catch(FileNotFoundException e)
    {
        System.out.println("Error Finding File: " + e.getMessage());
    }
    catch(IOException e)
    {
        System.out.println("Error Saving Test: " + e.getMessage());
        e.printStackTrace();
    }
    catch(NullPointerException e)
    {
        System.out.println("Error: " + e.getMessage());
    }
}



//------------------------------------------------------------------------------
/// Method : loadDocument
/// @Description - loads documentName into an object
/// @param[in] <documentName> - the name of the document to load
/// @return <void>
//------------------------------------------------------------------------------

public AbstractQuestionContainer loadDocument() throws IOException
{
    container = new AbstractQuestionContainer();

    try
    {
        FileInputStream inputStream = new FileInputStream(workingFile);
        ObjectInputStream objectInput = new ObjectInputStream(inputStream);

       container = (AbstractQuestionContainer) objectInput.readObject();

       objectInput.close();
    }
    catch(ClassNotFoundException e)
    {
        System.out.println("ClassNotFoundException: " + e.getMessage());
    }
    catch(EOFException e)
    {
        // do nothing
    }

    return container;
}

同样,我需要对加载对象使用的唯一功能是能够打印出与Test相关的问题及其可能的答案。当我在Driver类中进行printDocument调用时,不会显示任何内容。对我来说,这表明该对象已加载,但未以某种方式正确加载。

我已经搜索了与对象序列化有关的Stack,并且没有找到任何有助于此事的东西。非常感谢任何和所有的帮助。感谢。

编辑:以下是代码的调用方式:

    if(buildBuffer.size() == 1)
    {
        AbstractQuestionContainer document = buildBuffer.get(0);
        loader.saveDocument(document);
        buildBuffer.remove(0);

        System.out.println(documentToSave + " has been saved.");
        System.out.println();
    }
    else
    {
        // loop over build buffer and find file
        for(int i = 0; i < buildBuffer.size(); i++)
        {   
            if(buildBuffer.get(i).fileName().equals(loader.getFileName()))
            {
                loader.saveDocument(buildBuffer.get(i));
buildBuffer.remove(i);                                                          
            }
        }

        System.out.println(documentToSave + " has been saved.");
        System.out.println();
    }

加载:

// if the size of the build buffer is 1, then there's only one thing to load, so load it.
if(buildBuffer.size() == 1)
{
    loadBuffer.add(buildBuffer.get(0));
    buildBuffer.remove(0);

    System.out.println("Document " + documentToLoad + " loaded to the buffer successfully");
    System.out.println();
    break;
}
else
{
    for(int i = 0; i < buildBuffer.size(); i++)
    {
        if(buildBuffer.get(i).fileName().equals(loader.getFileName()))
        {
            loadBuffer.add(buildBuffer.get(i));
            buildBuffer.remove(0);

            System.out.println("Document " + documentToLoad + " loaded to the buffer successfully");
            System.out.println();
            break;
        }
    }
}

// check to see if given file is in local directory
boolean exists = new File(filePath, documentToLoad).exists();

// if the file exists, load it, and add it to the loadBuffer
if(exists)
{
    container = loader.loadDocument();
    loadBuffer.add(container);

    System.out.println("Document " + documentToLoad + " loaded to the buffer successfully");
    System.out.println();
}
else
{
    System.out.println("Error: File entered does not exist in either the buildBuffer or local directory.");
    System.out.println();
    continue;
}

0 个答案:

没有答案