com.github.rrsunhome.excelsql.viewer.FileViewer Maven / Gradle / Ivy
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();
}
}));
}
}