Sirdavos
Sirdavos

Reputation: 85

File operation problem with ObjectOutputStream/ObjectInputStream

I'm trying to write couple of object using ObjectOutputStream to a file. After that, I read the file with ObjectInputStream. The problem is, I can only read first object in the file. When I opened the file with notepad++, I can see the entries for other objects.

Here writer part

public class FileProcess {
    private ObjectOutputStream output;
    private Accounts account;
    public FileProcess() {}

    public void WriteObject(Accounts account) {
        this.account = account;
        openFile();
        addRecords();
        closeFile();

    }

    private void openFile() {
        try {
            output = new ObjectOutputStream(Files.newOutputStream(Paths.get("record_files.txt"),CREATE,APPEND));
        }catch (Exception e) {
            System.out.println("\nError opening file");
        }
    }
    private void addRecords() {
        try {
            output.writeObject(account);
        }
        catch (Exception e) {
            System.out.printf("Error writing file %s",e);
        }
    }

    private void closeFile() {
        try {
            if(output != null)
                output.close();
        } catch (Exception e) {
            System.out.println("\nError closing file");
        }
    }
}

And Reader part

abstract class User {
    private String userID;
    private int password;
    private String position;
    private ObjectInputStream input;

    public User(String userID, int password, String position) {
        this.userID = userID;
        this.password = password;
        this.position = position;
    }


     boolean signIn() {

        boolean found_check = false;
        openFile();
        found_check = checkRecords();
        closeFile();
        return found_check;
    }

    private void closeFile() {
        try {

            if(input != null)
                input.close();

        } catch (Exception e) {
            System.out.println("Error closing file");
        }

    }


    private boolean checkRecords() {
        try {
            while(true) {
                Accounts account = (Accounts) input.readObject();
                System.out.printf("%s %d %s",account.getUserID(),account.getPassword(),account.getPosition());
            }
        }
        catch (Exception e) {
            System.out.println("\nError reading file");
            return false;
        }
        return false;
    }


    private void openFile() {
        try {
            input = new ObjectInputStream(Files.newInputStream(Paths.get("record_files.txt")));

        }catch (Exception e) {
            System.out.println("Error opening file");
        }
    }
}

Accounts class implements Serializable

public class Accounts implements Serializable {

    private String userID;
    private int password;
    private String position;

    public Accounts(String userID, int password, String position) {
        try {

            this.userID = userID;
            this.password = password;
            this.position = position;
        }
        catch (Exception e) {
            System.out.printf(e.toString());
        }
    }
}

Upvotes: 0

Views: 66

Answers (1)

ShaneCoder
ShaneCoder

Reputation: 781

Your code is throwing the following exception: java.io.StreamCorruptedException: invalid type code: AC.
There's a good question and answer here

This was discovered by adding e.printStackTrace(); to the exception handler in User.checkRecords.

Upvotes: 1

Related Questions