Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中保存和加载对象?我试过了,但它一直抛出IOexception_Java_Serialization - Fatal编程技术网

如何在Java中保存和加载对象?我试过了,但它一直抛出IOexception

如何在Java中保存和加载对象?我试过了,但它一直抛出IOexception,java,serialization,Java,Serialization,我不知道为什么IOException会被抓到。因为我已经创建了该文件并通过ObjectOutputStream运行它。例外消息是: java.io.NotSerializableException:cafesys.Order 可能是因为文件很糟糕?它说:文件糟糕的写入被中止;java.io.NotSerializableException:cafesys.Order。它的意思是:第一,你的文件很烂,第二,cafesys.Order是不可序列化的。找到咖啡馆。订购并使其可连载。哈哈@CocoNes

我不知道为什么IOException会被抓到。因为我已经创建了该文件并通过ObjectOutputStream运行它。例外消息是:

java.io.NotSerializableException:cafesys.Order


可能是因为文件很糟糕?它说:文件糟糕的写入被中止;java.io.NotSerializableException:cafesys.Order。它的意思是:第一,你的文件很烂,第二,cafesys.Order是不可序列化的。找到咖啡馆。订购并使其可连载。哈哈@CocoNess真的很有趣。。。

public class DriverClass implements Serializable {
    CafeManager cafe = new CafeManager(); - Creates the object 

    public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException{
        CafeManager newcafe;

        newcafe = load(); - Calls the load module

        SystemMenu menu = new SystemMenu();
        menu.displaymenu(); - Running of the menu


        newcafe = new CafeManager(newcafe.getMenuItems(),newcafe.getOrders()); - Once the user exits the menu, file should be saved


        save(newcafe);

    }

    public static void save(CafeManager newcafe) throws FileNotFoundException, IOException{
        try{
            FileOutputStream fileOut = new FileOutputStream("system.dat");

            ObjectOutputStream out = new ObjectOutputStream(fileOut);

            out.writeObject(newcafe);
        }
        catch (FileNotFoundException fe){
            System.out.println("File Not found.");
        }
        catch (IOException ie){
            System.out.println("File Crappy" + ie.getMessage()); //Output: File Crappycafesys.Order
        }

    }

    public static CafeManager load() throws FileNotFoundException, IOException, ClassNotFoundException{
        CafeManager curcafe = new CafeManager();
        FileInputStream fileIn = new FileInputStream("system.dat");

        ObjectInputStream in = new ObjectInputStream(fileIn);

        try{
            curcafe = (CafeManager) in.readObject();
        }
        catch(ClassCastException | ClassNotFoundException ce){
            //
        }
        catch (FileNotFoundException fe){
            System.out.println("File Not found.");
        }
        catch (IOException ie){
            System.out.println("File Crappy" + ie.getMessage()); //Output: File Crappy writing aborted; java.io.NotSerializableException: cafesys.Order
        }

        return curcafe;
    }