Package sg.edu.nus.iss.se07.common.io

Source Code of sg.edu.nus.iss.se07.common.io.CSV

/**
*
* @(#)CSV.java    1.5 21/03/2009
* Copyright (c) 2009 by Team 07(SE17) @ISS-NUS. All Rights Reserved.
*
**/

package sg.edu.nus.iss.se07.common.io;

import sg.edu.nus.iss.se07.common.exceptions.AppException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Isak
*/
public class CSV {

    private boolean initialized = false;
    private boolean closing = true;
    private boolean closed = false;
    private Charset charset = Charset.forName("ISO-8859-1");
    private PrintWriter outputStream = null;

    public CSV(CSVFormat format) {
        setCSVformat(format);
    }
    private CSVFormat CSVformat;

    /**
     * Get the value of CSVformat
     *
     * @return the value of CSVformat
     */
    public CSVFormat getCSVformat() {
        return CSVformat;
    }

    /**
     * Set the value of CSVformat
     *
     * @param CSVformat new value of CSVformat
     */
    public void setCSVformat(CSVFormat CSVformat) {
        this.CSVformat = CSVformat;
    }

    public void initWriting(String filename, boolean append) throws AppException {
        if (!closed) {
            if (!initialized) {
                if (filename != null) {
                    try {
                        outputStream = new PrintWriter (new OutputStreamWriter(new FileOutputStream(filename, append), charset));
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(CSV.class.getName()).log(Level.SEVERE, null, ex);
                        throw new AppException(ex, CSV.class.getName(), ex.getMessage());
                    }
                }
                initialized = true;
            }
        }
    }

    public void doneWriting() {
        if (!closed) {
            if (closing) {
                charset = null;
            }
            try {
                if (initialized) {
                    outputStream.close();
                }
            } catch (Exception e) {
                // just eat the exception
                e.printStackTrace();
            }
            outputStream = null;
            closed = true;
        }

    }

    public void flushWriter() {
        if (outputStream != null) {
            outputStream.flush();
        }
    }

    public boolean write(String data) throws AppException {
        boolean result = true;
        if (!initialized) {
            result = false;
        } else {
            if (data == null) {
                data = "";
            }
            outputStream.write(data);
        }
        return result;
    }



    public String read(String filename) {
        String data = null;
        return data;
    }

    @Override
    protected void finalize() throws Throwable {
        closed = true;
    }
}
TOP

Related Classes of sg.edu.nus.iss.se07.common.io.CSV

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.