package com.olm.util; import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import lombok.extern.slf4j.Slf4j; import java.io.*; import java.nio.charset.StandardCharsets; import java.sql.Struct; import java.util.Date; /** * ClassName:FileUtilText Package:com.olm.util Date:2020/8/10 Auth:penghui@olm.com.cn */ @Slf4j public class FileUtilText { public static boolean writeTxtFile(String content){ String logFile = Configure.getValue("logoutpath"); if (StrUtil.contains(logFile, "2022")){ logFile = logFile.replace("2022", DateUtil.format(DateUtil.date(), "YYYY")); } File outFile = new File(logFile); String logPath= outFile.getParent(); File filePath = new File(logPath); if(!filePath.exists() && !filePath.isDirectory()){ boolean mkOk= filePath.mkdirs(); if (mkOk){ log.info("导出文件夹不存在,自动创建成功!"); } } if (!outFile.exists()){ try { boolean createOk= outFile.createNewFile(); if (createOk){ log.info("导出记录不存在,自动创建成功!"); } }catch (IOException e){ log.error("日期文件创建失败", e); } } FileOutputStream fOut; fOut = null; boolean result = false; try { fOut = new FileOutputStream(outFile, true); fOut.write((DateUtil.now() +"\t"+ content + " \r\n").getBytes(StandardCharsets.UTF_8)); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (fOut != null) { try { fOut.close(); } catch (IOException e) { log.error("写入文件出错",e); } } } return result; } }