Skip to content
Advertisement

How to Serialize and Deserialize multiple objects in Java during runtime

I am creating a Login Form and Sign up Form using JFrames. The problem is every time when the user presses the Sign up button the user details should be serialized and added to the “users.ser” file. And if the user presses the Login button it has to deserialize the data from that file and check for granting access to the user.

Here is the code I tried and it causes EOFException.

For registering the user into the file.

    void registerUser(User user) {
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/Atom workspace/Java_programs/Form/users.ser"));
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/Atom workspace/Java_programs/Form/users.ser"));
            ArrayList<User> users = new ArrayList<>();
            try {
                users = (ArrayList<User>) ois.readObject();
            } catch (EOFException e) {
                //If the file is empty 
                users.add(new User("User1", "Pass1"));
                users = new ArrayList<User>();            
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {        
                //If the file has some contents add the new user into the file       
                users.add(user);
                oos.writeObject(users);  
                oos.close(); 
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }  

For Checking the user is in the file or not.

    boolean validateUser(User user) {
        ArrayList<User> users = new ArrayList<>();
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/Atom workspace/Java_programs/Form/users.ser"));
            users = (ArrayList<User>) ois.readObject();
            for (User tmpUser : users) {
                if(user.name.equals(tmpUser.name) && user.password.equals(tmpUser.password)) 
                    return true;
            }
        } catch(EOFException e) {
            System.out.println("EOFException occured"); 
            return false;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }  

Error Message when the registerUser(user) is called :

java.io.EOFException
        at java.base/java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2926)     
        at java.base/java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3421)
        at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:959)
        at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:397)
        at Java_programs.Form.LoginForm.registerUser(FormMain.java:119)
        at Java_programs.Form.MyFieldListener.actionPerformed(FormMain.java:73)
        at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
        at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
        at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
        at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
        at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
        at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
        at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
        at java.desktop/java.awt.Component.processEvent(Component.java:6391)
        at java.desktop/java.awt.Container.processEvent(Container.java:2266)
        at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
        at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
        at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
        at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
        at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)    
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)    
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)    
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

When calling validateUser(User user) :

   EOFxception Occured
   No access

I don’t know why its if throwing even though I caught the exception in void registerUser(User user) method.

I am actually a newbie please explain it to me. And Thanks in advance 🙂

Advertisement

Answer

You are truncating the input file before you read it in, as you have setup file input and output on consecutive lines:

new FileInputStream("D:/Atom workspace/Java_programs/Form/users.ser"));
new FileOutputStream("D:/Atom workspace/Java_programs/Form/users.ser");

Move your writing code to a separate method – called after closing the input file – and never write to your master file directly. Always output to a temp file first and rename the file afterwards to replace the original.

Fix your exception handling to use try with resources.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement