//创建文件夹
String rootPath = "path";
String dirPath = new StringBuilder(rootPath).append(File.separator).append("yyyyMMdd").toString();
File root = new File(rootPath);
if (!root.exists()) {
root.mkdir();
}
File dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdir();
}
//创建文件
String writePathTxt = new StringBuilder(dirPath).append(File.separator).append(FileName).toString();
File FileTxt = new File(writePathTxt);
if (!FileTxt.exists()) {
FileTxt.createNewFile();
}
//写文件
if (!StringUtil.isEmpty(content)) {
//ByteBuffer bf = ByteBuffer.allocate(1024);
FileOutputStream os = null;
FileChannel channel = null;
try {
os = new FileOutputStream(writePathTxt , true);
channel = os.getChannel();
channel.write(ByteBuffer.wrap((content + System.getProperty("line.separator")).getBytes()));
channel.force(true);
} catch (IOException e) {
e.printStackTrace();
throw e;
}finally {
try {
if (channel != null) {
channel.close();
}
} catch (IOException e) {
logger.error("关闭 FileChannel 异常", e);
}
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
logger.error("关闭 FileOutputStream 异常", e);
}
}
}
}
写文件数据逻辑
最新推荐文章于 2021-12-03 18:38:33 发布