Singleton Serializable Database save method throws IOException

22 views Asked by At

The goal of this class is to be able to load when the program starts and save when data is changed. I have used these same methods before, the save and load ones. For some reason I can not get it to work now? Any ideas?

package BackEnd;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Hashtable;


public class DataBase implements Serializable{
    
    private static final long serialVersionUID = 1L;

    private static Hashtable<String, Account> accountTable = new Hashtable<String, Account>();
    
    private static int nextAccountid = 1;
    
    private static DataBase instance = new DataBase();
    
    AccountInstance account = AccountInstance.getInstance(); 
    
    private DataBase() {
        
    }
    
    public static DataBase getInstance() {
        
        return instance;
    }
    
    public static DataBase load() {
        try (ObjectInputStream ois = new ObjectInputStream(
                new BufferedInputStream(new FileInputStream(new File("./src/Data/data.dat"))))) {
            instance = (DataBase) ois.readObject();
            System.out.println("load");
        } catch (IOException | ClassNotFoundException e) {
            getInstance();
            System.out.println("error load");
        }
        return instance;
    }
    
    public static void save() {
        try (ObjectOutputStream oos = new ObjectOutputStream(
                new BufferedOutputStream(new FileOutputStream(new File("./src/Data/data.dat"))))) {
            oos.writeObject(instance);
            System.out.println("Save");
        } catch (IOException e) {
            System.out.println("Error Save");
        }
    }
}

The save and load method are not working, what am I doing wrong?

0

There are 0 answers