Package phonetalks.io

Source Code of phonetalks.io.BinaryFileIndexSaver

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package phonetalks.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import phonetalks.entities.collections.CardIndex;

/**
*
* @author Платон
*/
public class BinaryFileIndexSaver extends AbstarctIndexSaver {

    private String inputPath;
    private String outputPath;

    public BinaryFileIndexSaver(String input, String output) {
        inputPath = input;
        outputPath = output;
    }

    @Override
    public boolean writeIndex(CardIndex index) {
        FileOutputStream fout = null;
        try {
            fout = new FileOutputStream(outputPath);
            ObjectOutputStream objOut = new ObjectOutputStream(fout);
            objOut.writeObject((Object) index);
        } catch (IOException ioExc) {
            System.out.println(ioExc);
            return false;
        } finally {
            if (fout != null) {
                try {
                    fout.close();
                } catch (IOException ex) {
                    Logger.getLogger(BinaryFileIndexSaver.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return true;
    }

    @Override
    public CardIndex readIndex() {
        FileInputStream fin = null;
        CardIndex result = null;
        try {

            fin = new FileInputStream(inputPath);
            ObjectInputStream objIn = new ObjectInputStream(fin);
            result = (CardIndex) objIn.readObject();
        } catch (IOException ioExc) {
            System.out.println(ioExc);
        } catch (ClassNotFoundException cnfExc) {
            System.out.println(cnfExc);
        } finally {
            if (fin != null) {
                try {
                    fin.close();
                } catch (IOException ex) {
                    Logger.getLogger(BinaryFileIndexSaver.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return result;
    }
}
TOP

Related Classes of phonetalks.io.BinaryFileIndexSaver

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.