版权声明:@By Author Hadoop.W https://blog.csdn.net/u013786479/article/details/82592743
package org.wxz.tools.oracle.io.file.util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.InputStream;
import org.wxz.tools.oracle.empty.util.EmptyUtil;
import org.wxz.tools.oracle.exception.print.util.ExceptionPrintUtil;
import org.wxz.tools.oracle.io.callback.IOReadCallback;
import org.wxz.tools.oracle.io.util.IOCloseUtil;
import org.wxz.tools.oracle.io.util.IOOpenUtil;
import org.wxz.tools.oracle.io.util.IOReadUtil;
/**
* 文件制作工具类
*
* @author XiongZhi.Wu 2017年12月10日
*/
public abstract class FileMakeUtil {
// 默认是否要覆盖
private static final boolean DEFAULT_COVER = false;
/**
* 制作
*
* @author XiongZhi.Wu 2017年12月10日
* @param file
* 文件路径
* @return
*/
public static boolean makeFolder(String file) {
return makeFolder(file, DEFAULT_COVER);
}
/**
* 制作
*
* @author XiongZhi.Wu 2017年12月10日
* @param file
* 文件路径
* @param cover
* 是否覆盖
* @return
*/
public static boolean makeFolder(String file, boolean cover) {
return makeFolder(new File(file), cover);
}
/**
* 制作
*
* @author XiongZhi.Wu 2017年12月10日
* @param file
* 文件对象
* @return
*/
public static boolean makeFolder(File file) {
return makeFolder(file, DEFAULT_COVER);
}
/**
* 制作
*
* @author XiongZhi.Wu 2017年12月10日
* @param file
* 文件对象
* @param cover
* 是否覆盖
* @return
*/
public static boolean makeFolder(File file, boolean cover) {
return makeAll(file, cover, true);
}
/**
* 制作
*
* @author XiongZhi.Wu 2017年12月10日
* @param file
* 文件路径
* @return
*/
public static boolean makeFile(String file) {
return makeFile(file, DEFAULT_COVER);
}
/**
* 制作
*
* @author XiongZhi.Wu 2017年12月10日
* @param file
* 文件路径
* @param cover
* 是否覆盖
* @return
*/
public static boolean makeFile(String file, boolean cover) {
return makeFile(new File(file), cover);
}
/**
* 制作
*
* @author XiongZhi.Wu 2017年12月10日
* @param file
* 文件对象
* @return
*/
public static boolean makeFile(File file) {
return makeFile(file, DEFAULT_COVER);
}
/**
* 制作
*
* @author XiongZhi.Wu 2017年12月10日
* @param file
* 文件对象
* @param cover
* 是否覆盖
* @return
*/
public static boolean makeFile(File file, boolean cover) {
return makeAll(file, cover, false);
}
/**
* 制作
*
* @author XiongZhi.Wu 2017年12月10日
* @param file
* 文件对象
* @param cover
* 是否覆盖
* @param type
* 文件类型
* @return
*/
private static boolean makeAll(File file, boolean cover, boolean type) {
if (cover) {
boolean flag = FileDeleteUtil.delete(file);
if (flag) {
return makeAll(file, type);
} else {
return false;
}
} else {
if (EmptyUtil.isNull(file)) {
return makeAll(file, type);
} else {
return true;
}
}
}
/**
* 制作
*
* @author XiongZhi.Wu 2017年12月10日
* @param file
* 文件对象
* @param type
* 文件类型
* @return
*/
private static boolean makeAll(File file, boolean type) {
try {
return type ? file.mkdirs() : (FileMakeUtil.makeFolder(FileFolderUtil.folder(file)) ? file.createNewFile() : false);
} catch (Exception e) {
throw new RuntimeException(ExceptionPrintUtil.initExceptionPrintModel(e, "File Make Cause Exception."));
}
}
/**
* 制作
*
* @author XiongZhi.Wu 2017年12月13日
* @param input
* 输入对象
* @param file
* 文件对象
* @param cover
* 是否覆盖
* @return
*/
public static boolean serialize(Object input, File file, boolean cover) {
if (FileCoverUtil.needCover(file, cover)) {
BufferedOutputStream bos = IOOpenUtil.initBufferedOutputStream(file);
try {
if (input instanceof Byte[]) {
bos.write((byte[]) input);
} else if (input instanceof InputStream) {
IOReadUtil.loopRead((InputStream) input, new IOReadCallback<Integer>() {
@Override
public void service(Integer bit) {
try {
bos.write(bit);
} catch (Exception e) {
throw new RuntimeException(ExceptionPrintUtil.initExceptionPrintModel(e, "OutputStream Write Cause Exception."));
}
}
});
} else {
throw new RuntimeException(ExceptionPrintUtil.initExceptionPrintModel("No Such Input Type Cause Exception."));
}
} catch (Exception e) {
throw new RuntimeException(ExceptionPrintUtil.initExceptionPrintModel(e, "File Serialize Cause Exception."));
} finally {
IOCloseUtil.closeOutputStream(bos);
}
}
return true;
}
}