Package bpntojava.model

Source Code of bpntojava.model.SaveData

package bpntojava.model;

import bpntojava.model.bpndata.Data;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Daten Laden und speichern.
*
* @author mschuessler
*/
public class SaveData {

    /**
     * Konstruktor.
     */
    private SaveData() {
    }

    /**
     * Überprüft ob eine datei existiert.
     *
     * @param filename Dateiname
     * @return true = Datei existiert
     */
    public static boolean dataExists(String filename) {
        File datei = new File(filename);
        if (datei.exists()) {
            return true;
        }
        return false;
    }

    /**
     * Speichert eine Datenbank in eine Datei.
     *
     * @param data Datenbank
     * @param filename Dateiname
     */
    public static void save(Data data, String filename) {
        FileOutputStream fos;
        ObjectOutputStream out;
        try {
            fos = new FileOutputStream(filename);
            out = new ObjectOutputStream(fos);
            out.writeObject(data);
            out.close();
        } catch (IOException ex) {
            System.out.println("Save geht nicht.");
        }
    }

    /**
     * Lädt eine Datenbank aus einer Datei.
     *
     * @param filename Dateiname
     * @return Datenbank
     */
    public static Data load(String filename) {
        Data data = null;
        FileInputStream fis;
        ObjectInputStream in;
        try {
            fis = new FileInputStream(filename);
            in = new ObjectInputStream(fis);
            try {
                data = (Data) in.readObject();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(SaveData.class.getName()).log(Level.SEVERE, null, ex);
            }
            in.close();
        } catch (IOException ex) {
            System.out.println("Load geht nicht.");
        }
        return data;
    }
}
TOP

Related Classes of bpntojava.model.SaveData

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.