Create serializable file once in java

1

My problem is that I want to create a System-type object only the first time I start the program, the next time I want to read the saved System file.

 public static void main(String[] args) {

        Sistema sistema;
        try {
            sistema = new Sistema();

            FileOutputStream file = new FileOutputStream("archivo_sistema.txt");
            BufferedOutputStream bf = new BufferedOutputStream(file);
            ObjectOutputStream obj = new ObjectOutputStream(bf);

            obj.writeObject(sistema);
            obj.flush();
            obj.close();

            FileInputStream recuperar_file = new FileInputStream("archivo_sistema.txt");
            BufferedInputStream bi = new BufferedInputStream(recuperar_file);
            ObjectInputStream obs = new ObjectInputStream(bi);

            sistema = (Sistema)(obs.readObject());

        } catch (ClassNotFoundException | IOException e) {
            e.printStackTrace();
        }
        JFrameMenu vMenu = new JFrameMenu(sistema);

        vMenu.setVisible(true);

    }
}
    
asked by Francisco Aguilar 23.11.2018 в 19:56
source

1 answer

1

Saved where? During program execution, the system variable remains in the heap memory. When the program ends, these data cease to exist. Therefore, if you finish the execution of the program and re-launch it, the system variable is no longer in place, until it is redefined. If you want to keep it, you have to save it somewhere: cache, database ...

    
answered by 26.11.2018 в 16:49