All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rrsunhome.excelsql.viewer.FileViewer Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
package com.github.rrsunhome.excelsql.viewer;

import java.io.*;

/**
 * @author : qijia.wang
 * create at:  2020/3/31  下午3:45
 */
public class FileViewer extends AbstractStreamViewer {

    public FileViewer(String outPutPath, boolean append) throws IOException {
        this(new FileOutputStream(outPutPath, append));
    }

    public FileViewer(String outPutPath) throws IOException {
        this(outPutPath, false);
    }

    public FileViewer(File file) throws IOException {
        this(new FileOutputStream(file));
    }

    protected FileViewer(Writer writer) {
        super(writer);
    }

    private FileViewer(OutputStream os) {
        super(new BufferedOutputStream(os));
        registerFileCloseHook();
    }

    @Override
    public void close() throws IOException {
        if (writer != null) {
            flushAndClose();
        }

    }

    private void flushAndClose() throws IOException {
        this.writer.flush();
        this.writer.close();
    }

    private void registerFileCloseHook() {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                this.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }));
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy