0

嘿,我在保存和加载我的 planeStore 时遇到了运行时错误。保存选项将存储保存到用户选择的文本文件名中,然后在用户输入文件名时加载。

这是错误:

at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at java.util.TreeMap.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at java.util.HashMap.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at FileUtility.save(FileUtility.java:27)
    at MainApp.main(MainApp.java:274)

文件实用程序类

import java.io.FileInputStream; 
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


/**
  * <i>Use this class to save and load any type of object (i.e. a container class (e.g. PersonStore), an account, a task, a person)
 *   @author     NMCG
 *   @version    1.0            
*/
public class FileUtility /*serialization - step 2*/
{

    /**
     * @param fileName relative or absolute file path (e.g. "name.txt" or "c:\\temp\\name.txt"
     * @param obj      address of any object to be stored (i.e. a container class (e.g. PersonStore), an account, a task, a person)
     */

    public static void save(String fileName, Object obj)
    {
        try
        {
            FileOutputStream fos = new FileOutputStream(fileName);
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(obj);
            oos.flush();
            oos.close();
            fos.close();
        }
        catch(Exception e)
        {
            System.out.println("Something bad happened during the save phase!");
            e.printStackTrace();
        }
    }

    /**
     * @param  fileName relative or absolute file path (e.g. "name.txt" or "c:\\temp\\name.txt")
     * @return Object   address of the object loaded into RAM from file
     */

    public static Object load(String fileName)  // "test.txt"
    {
        Object objectIn = null;
        try
        {
            FileInputStream fis = new FileInputStream(fileName);
            ObjectInputStream ois = new ObjectInputStream(fis);

            objectIn = ois.readObject();

            ois.close();
            fis.close();
        }
        catch(Exception e)
        {
            System.out.println("Something bad happened during the save phase!");
            e.printStackTrace();
        }
        return objectIn;
    }
}

店铺

case 9:
                    System.out.println("Store");
                    int storeChoice;
                    storeChoice = MenuMethods.getMenuChoice(
                                                    "1.Save." +
                                                    "\n2.Load.", 2,
                                                    "Please enter your choice:", "Error [1,2] Only");
                    switch (storeChoice) 
                    {
                    case 1:
                    System.out.println("Save");
                    String inputSave;
                    System.out.println("Please enter the save name:");
                    inputSave = keyboard.next();
                    System.out.println("Saving..............");
                    PlaneStore copyMyStore = airlineMap.copy();
                    System.out.println(copyMyStore);
                    //shallow copy would look like this...
                    PlaneStore shallowCopyMyStore;
                    //both variables point to same object
                    shallowCopyMyStore = airlineMap; 
                    //lets store and re-load the mystore object
                    FileUtility.save(inputSave, airlineMap);

                        break;

                    case 2:
                        System.out.println("Load");
                        String inputLoad;
                        System.out.println("Please enter the file name you want to load:");
                        inputLoad = keyboard.next();
                        System.out.println("Loading..............");
                        PlaneStore loadedStore = (PlaneStore)FileUtility.load(inputLoad);
                        System.out.println("\n--- RELOADED STORE CONTENTS ---");
                        loadedStore.print();
                    break;
                    }
                    break;

任何人都可以看到有什么问题吗?

4

0 回答 0