matrix.boot.common.utils.BIOStreamUtil Maven / Gradle / Ivy
package matrix.boot.common.utils;
import matrix.boot.common.exception.ServiceException;
import java.io.*;
/**
* BIO流处理工具
* @author wangcheng
*/
public class BIOStreamUtil {
/**
* 流转字符串
* @param is 输入流
* @return 字符串
*/
public static String streamToString(InputStream is) {
StringBuilder result = new StringBuilder();
InputStreamReader isr = null;
BufferedReader br = null;
try {
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
result.append(line).append("\n\r");
}
} catch (Exception e) {
throw new ServiceException(e);
} finally {
closeStream(br);
closeStream(isr);
}
return result.toString();
}
/**
* 流转文件
* @param is 输入流
* @param filePath 文件路径
* @return 文件
*/
public static File streamWriteFile(InputStream is, String filePath) {
return BIOStreamUtil.streamWriteFile(is, filePath, null);
}
/**
* 流输出文件
* @param is 输入流
* @param filePath 文件夹路径
* @param fileName 文件名称
* @return 文件
*/
public static File streamWriteFile(InputStream is, String filePath, String fileName) {
FileOutputStream fos = null;
File file = StringUtil.isEmpty(fileName) ? new File(filePath) : new File(filePath, fileName);
try {
FileUtil.createNewFile(file);
fos = new FileOutputStream(file);
streamWriteStream(is, fos);
} catch (Exception e) {
throw new ServiceException(e);
} finally {
closeStream(fos);
}
return file;
}
/**
* 文件输出到流
* @param file 文件
* @param os 输出流
*/
public static void fileWriteStream(File file, OutputStream os) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
streamWriteStream(fis, os);
} catch (Exception e) {
throw new ServiceException(e);
} finally {
closeStream(fis);
}
}
/**
* 流输出流
* @param is 输入流
* @param os 输出流
*/
public static void streamWriteStream(InputStream is, OutputStream os) {
try {
byte[] b = new byte[4096];
int len;
while ((len = is.read(b)) > 0) {
os.write(b, 0, len);
}
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 文件复制
* @param sourceFilePath 原文件路径
* @param distFilePath 新文件路径
* @return 新文件
*/
public static File fileWriteFile(String sourceFilePath, String distFilePath) {
File file = new File(sourceFilePath);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
return BIOStreamUtil.streamWriteFile(fis, distFilePath);
} catch (Exception e) {
throw new ServiceException(e);
} finally {
BIOStreamUtil.closeStream(fis);
}
}
/**
* 文本输出到文件
* @param text 文本
* @param filePath 文件路径
* @return 文件
*/
public static File stringWriteFile(String text, String filePath) {
return BIOStreamUtil.stringWriteFile(text, filePath, null);
}
/**
* 文本输出到文件
* @param text 文本
* @param filePath 文件路径
* @param fileName 文件名
* @return 文件
*/
public static File stringWriteFile(String text, String filePath, String fileName) {
File file = StringUtil.isEmpty(fileName) ? new File(filePath) : new File(filePath, fileName);
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
FileUtil.createNewFile(file);
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos);
osw.write(text);
return file;
} catch (Exception e) {
throw new ServiceException(e);
} finally {
BIOStreamUtil.closeStream(osw);
BIOStreamUtil.closeStream(fos);
}
}
/**
* 关闭流
* @param closeable 实现关闭接口的类
*/
public static void closeStream(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (Exception ignored) {
}
}
}