首页 文章

Java EOFException问题ObjectInputStream

提问于
浏览
0

我正在尝试使用与HashMap成功的方法类似的方法实现“帐户”列表的加载/保存 .

Background:

private List<Account> accounts;

public AccountManager() {
    try {
        accounts = LoadSave.loadAccounts();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

在这里,我想设置我的帐户列表以包含已保存的帐户 .

Load/Save Implementation:

public static void saveAccounts(List<Account> accounts) throws IOException {
    try {
        FileOutputStream fos = new FileOutputStream("accounts.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(accounts);
        oos.close();
        fos.close();
        System.out.printf("Saved account.");
    }
    catch(IOException ioe) {
        ioe.printStackTrace();
    }
}

public static List<Account> loadAccounts() throws IOException {
    List<Account> accounts = null;
    try {
        FileInputStream fis = new FileInputStream("accounts.ser");
        ObjectInputStream ois = new ObjectInputStream(fis);
        accounts = (List<Account>) ois.readObject();
        ois.close();
        fis.close();
    }
    catch(IOException ioe) {
        ioe.printStackTrace();
    }
    catch(ClassNotFoundException c) {
        System.out.println("Class not found");
        c.printStackTrace();
    }

    return accounts;
}

The problem 是我第一次初始化AccountManager时,accounts.ser文件没有保存的帐户,因为该功能是在以后的方法中实现的,因此我得到了以下异常:

java.io.EOFException的在java.io.ObjectInputStream中$ PeekInputStream.readFully(ObjectInputStream.java:2681)在java.io.ObjectInputStream中$ BlockDataInputStream.readShort(ObjectInputStream.java:3156)在java.io.ObjectInputStream.readStreamHeader(ObjectInputStream的.java:862)java.io.ObjectInputStream . (ObjectInputStream.java:358)atatatatons.inventory_model.LoadSave.loadAccounts(LoadSave.java:69)at billing.AccountManager . (AccountManager.java:81)at sun.reflect位于sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)的sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)中的.NativeConstructorAccessorImpl.newInstance0(Native Method)at java.lang.reflect.Constructor.newInstance(Constructor) .java:423)at java.lang.Class.newInstance(Class.java:442)at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)at javafx.fxml.FXMLLoader $ ValueElement.processAttribute(FXMLLoader . java:927)at javafx.fxml.FXM javafx.fxml.fXMLLoader上的LLoader $ InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)javafx.fxml.fxml.FXML上的javafx.fxml.FXMLLoader $ ValueElement.processStartElement(FXMLLoader.java:744)中的$ Element.processStartElement(FXMLLoader.java:240) .FXMLLoader.processStartElement(FXMLLoader.java:2707)在javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)在javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)在javafx.fxml.FXMLLoader.load( FXMLLoader.java:2409)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sunat . .reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43),位于sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)的java.lang.reflect.Method.invoke(Method.java:498) . sun.reflect.DelegatingMethodAccess中的reflect.GeneratedMethodAccessor3.invoke(未知来源) orImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)at javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1771)在javafx.fxml.FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1657)在com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)在COM . com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)中的sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)位于com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher . java:59)com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)at com.sun.javafx.event com.sun.javaf上的.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) x.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java: 114)在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)在com.sun.javafx.event.BasicEventDispatcher com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)中的.dispatchEvent(BasicEventDispatcher.java:56)位于com的com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) . sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)at javafx.event.Event.fireEvent(Event.java:198)at javafx.scene.Node.fireEvent(Node.java:8411)at javafx.scene .control.Button.fire(Button.java:185)at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) at com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:96)at com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:89)at com位于com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)的com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent中的.sun.javafx.event.CompositeEventHandler $ NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) (EventHandlerManager.java:238)在com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)在com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent (BasicEventDispatcher.java:58)在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)在的com.sun . javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java: 114)在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)在com.sun.javafx.event.BasicEventDispatcher .dispatchEvent(BasicEve ntDispatcher.java:56)在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)在com.sun.javafx位于javafx.scene的javafx.sventne.Scene $ MouseHandler.process(Scene.java:3757)的javafx.event.Event.fireEvent(Event.java:198)上的.event.EventUtil.fireEvent(EventUtil.java:54) . 场景$ MouseHandler.access $ 1500(Scene.java:3485)在javafx.scene.cempl.processMouseEvent(Scene.java:1762)的javafx.scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2494)at com.sun . javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:394)在com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:295)在java.security.AccessController.doPrivileged(本地方法)at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleMouseEvent $ 353(GlassViewEventHandler.java:432)at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRende rLock(QuantumToolkit.java:389)at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)at com.sun.glass.ui.View.handleMouseEvent(View.java:555)at com .sun.glass.ui.View.notifyMouse(View.java:937)at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)at com.sun.glass.ui.win.WinApplication.lambda $ $ java(WinApplication.java:177)在java.lang.Thread.run(Thread.java:748)

我已经尝试将帐户设置为被捕获的EOFExceptions上的新ArrayList,但它仍然会抛出EOFException . 我希望能够在每次打开帐户管理器窗口时加载帐户,因此我需要能够在构造函数中加载帐户 .

1 回答

  • 0

    能够在填充文件时加载帐户或使用以下代码将帐户设置为空ArrayList:

    public static List<Account> loadAccounts() throws IOException {
        List<Account> accounts = null;
        try {
            File file = new File("accounts.ser");
            if (file.length() == 0) {
                accounts = new ArrayList<>();
            } else {
                FileInputStream fis = new FileInputStream("accounts.ser");
                ObjectInputStream ois = new ObjectInputStream(fis);
                accounts = (List<Account>) ois.readObject();
                ois.close();
                fis.close();
            }
        }
        catch(IOException ioe) {
            ioe.printStackTrace();
        }
        catch(ClassNotFoundException c) {
            System.out.println("Class not found");
            c.printStackTrace();
        }
        return accounts;
    }
    

    我只需要检查文件的长度,如果非零则返回对象,如果为零,则返回空的ArrayList .

相关问题