【问题标题】:File operation problem with ObjectOutputStream/ObjectInputStreamObjectOutputStream/ObjectInputStream 的文件操作问题
【发布时间】:2019-06-07 11:24:38
【问题描述】:

我正在尝试使用 ObjectOutputStream 将几个对象写入文件。之后,我使用 ObjectInputStream 读取文件。问题是,我只能读取文件中的第一个对象。当我用 notepad++ 打开文件时,我可以看到其他对象的条目。

这里是作者部分

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");
        }
    }
}

和阅读器部分

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 类实现 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());
        }
    }
}

【问题讨论】:

  • 你怎么知道它只读取一条记录?因为checkRecords 总是返回 false 或者您看到控制台中打印了一行?
  • 我看到控制台打印了一行

标签: java file objectinputstream objectoutputstream


【解决方案1】:

您的代码抛出以下异常:java.io.StreamCorruptedException: invalid type code: AC
There's a good question and answer here

这是通过将e.printStackTrace(); 添加到User.checkRecords 的异常处理程序中发现的。

【讨论】:

    猜你喜欢
    • 2013-08-29
    • 2015-01-05
    • 2023-03-12
    • 2011-07-18
    • 2014-10-18
    • 1970-01-01
    • 2016-04-11
    • 2012-05-29
    • 1970-01-01
    相关资源
    最近更新 更多