序列化,保存对象

时间:2016-11-23 22:04:39

标签: java

将对象保存到文件时遇到问题。我有类FileManager,其中包含将对象保存到文件的方法。此方法用于包含主循环的Control类(选择不同的选项)。我想通过选择选项EXIt保存对象但没有任何反应。当我添加新选项(即6 - 保存数据库)程序工作正常。我会感激任何可能出错的线索。

class FileManager {
    package utils;

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;

    import data.DataBase;

    public class FileManager {
      public static final String FILE_NAME = "file.txt";

      public void writeDataBaseToFile(DataBase db) {
        try (
                FileOutputStream fos = new FileOutputStream(FILE_NAME);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                ) {
            oos.writeObject(db);
        } catch (FileNotFoundException e) {
            System.err.println("Błąd");
        } catch (IOException e) {
            System.err.println("Błąd");
        }
    }
}

控制类:

class Control {
    package app;

    import data.DataBase;
    import data.Expense;
    import data.Income;
    import utils.AccountInfo;
    import utils.AddData;
    import utils.FileManager;
    import utils.Info;
    import utils.Options;

    public class Control {
    private AccountInfo account;
    private AddData addData;
    private DataBase dataBase;
    private Info inf;
    private Income income;
    private FileManager fileManager;

    public Control() {
        addData = new AddData();
        dataBase = new DataBase();
        inf = new Info(dataBase);
        account = new AccountInfo(dataBase);
        fileManager = new FileManager();
    }

    public void ControlLoop() {
        Options option;
        printOptions();
        while((option = Options.createOption(addData.getOption())) != Options.EXIT) {
            try {
                switch(option) {
                case ADD_INCOME:
                    addIncome();
                    break;
                case ADD_EXPENSE:
                    addExpense();
                    break;
                case PRINT_INCOME:
                    printIncome();
                    break;
                case PRINT_EXPENSE:
                    printExpense();
                    break;
                case RESUME_ACCOUNT:
                    resumeAccount();
                    break;
                case EXIT:
                    saveData();
                    }
            } catch(NullPointerException ex) {

            }
            printOptions();
        }
        addData.close();
    }

    public void addIncome() {
        income = addData.createIncome();
        dataBase.addBudget(income);
    }

    public void addExpense() {
        Expense expense = addData.createExpense();
        dataBase.addBudget(expense);
    }

    public void printIncome() {
        inf.printIncome();
    }

    public void printExpense() {
        inf.printExpense();
    }

    public void resumeAccount() {
        account.resumeIncome();
        account.resumeExpense();
    }

    public void saveData() {
        fileManager.writeDataBaseToFile(dataBase);
    }

    public void printOptions() {
        System.out.println("Wybierz opcję:");
        for(int i=0; i<6; i++) {
            System.out.println(Options.values()[i]);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码永远无法访问EXIT案例。

因为当optionEXIT时,它会终止循环。

while ((option=...) != Options.EXIT) {
    // execute loop body when option is not EXIT
    switch (option) {
    ...
    case Options.EXIT: // <-- it can simply not reach here. not ever.
        saveData();
}

尝试在while循环之外移动saveData()

while (...) { // process options
}
// We are exiting, save data.
saveData();
addData.close();

P.S。您需要在FileManager中关闭输出流。