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)) {
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);
}
}
}
}