import java.io.*;
import java.time.LocalDateTime;
public class IoUtil {
/**
* inputStream 保存文件 推荐使用 尽量使用 能用就用
*
* @param inputStream 文件流
* @param path 文件路径
* @return boolean
*/
public static boolean isSaved(String path, InputStream inputStream) {
FileOutputStream fileOutputStream = null;
try {
if (!isFileFloderCreated(path)) {
return false;
}
fileOutputStream = new FileOutputStream(path);
byte[] bytes = new byte[1024];
int singleByte;
while ((singleByte = (inputStream.read(bytes))) != -1) {
fileOutputStream.write(bytes, 0, singleByte);
}
fileOutputStream.flush();
return true;
} catch (Exception e) {
return false;
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
//以防inputStream会再次使用,这里不关闭
}
}
/**
* 创建文件夹
*
* @param path 文件夹路径
*/
public static boolean isFloderCreated(String path) {
File dir = new File(path);
// 判断文件夹是否存在,如果不存在则创建文件夹
if (dir != null && !dir.exists()) {
try {
dir.mkdirs();
return true;
} catch (Exception e) {
return false;
}
} else {
return true;
}
}
/**
* 创建文件目录
*
* @param path
*/
public static boolean isFileFloderCreated(String path) {
File file = new File(path);
File dir = file.getParentFile();
// 判断文件夹是否存在,如果不存在则创建文件夹
if (dir != null && !dir.exists()) {
try {
dir.mkdirs();
return true;
} catch (Exception e) {
return false;
}
} else {
return true;
}
}
/**
* 将文字写入文档
*
* @param path
* @param text
* @return
* @throws IOException
*/
public static boolean isTextSaved(String path, String text) {
text = LocalDateTime.now().toString()+"\r\n"+text+"\r\n\r\n";
FileOutputStream fileOutputStream = null;
BufferedWriter bufferWritter = null;
OutputStreamWriter outputStreamWriter = null;
try {
File file = new File(path);
if (file.exists()) {
file.delete();
}
isFileFloderCreated(path);
fileOutputStream = new FileOutputStream(file);
outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
bufferWritter = new BufferedWriter(outputStreamWriter);
bufferWritter.write(text);
fileOutputStream.flush();
outputStreamWriter.flush();
bufferWritter.flush();
return true;
} catch (Exception ex) {
return false;
} finally {
if (bufferWritter != null) {
try {
bufferWritter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStreamWriter != null) {
try {
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 写日志记录
*
* @param path
* @param text
* @return
* @throws IOException
*/
public static boolean isLogSaved(String path, String text) {
text = LocalDateTime.now().toString()+"\r\n"+text+"\r\n\r\n";
FileOutputStream fileOutputStream = null;
BufferedWriter bufferWritter = null;
OutputStreamWriter outputStreamWriter = null;
try {
File file = new File(path);
isFileFloderCreated(path);
fileOutputStream = new FileOutputStream(file, true);
outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
bufferWritter = new BufferedWriter(outputStreamWriter);
bufferWritter.write(text);
fileOutputStream.flush();
outputStreamWriter.flush();
bufferWritter.flush();
bufferWritter.close();
outputStreamWriter.close();
fileOutputStream.close();
return true;
} catch (Exception ex) {
return false;
} finally {
if (bufferWritter != null) {
try {
bufferWritter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStreamWriter != null) {
try {
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
转载请注明: 转载自 浮生一程
本文链接地址 Java 快速写文件工具类