/* 建立txt,并写入日志文件 */ public static void logWrite(String data, String name){ //System.out.println("写入"+name+"开始"); Calendar c = Calendar.getInstance();//可以对每个时间域单独修改 // int year = c.get(Calendar.YEAR); // int month = c.get(Calendar.MONTH); // int date = c.get(Calendar.DATE); // int hour = c.get(Calendar.HOUR_OF_DAY); // int minute = c.get(Calendar.MINUTE); // int second = c.get(Calendar.SECOND); // System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second); Date day=new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); data = "【写入时间:" + df.format(day) + "】:"+ data; byte[] sourceByte = data.getBytes(); String path = getConfig("userfiles.basedir"); FileOutputStream outStream =null; if(null != sourceByte){ try { File file = new File(path + name );//文件路径(路径+文件名) if (!file.exists()) { //文件不存在则创建文件,先创建目录 File dir = new File(file.getParent()); dir.mkdirs(); file.createNewFile(); } outStream = new FileOutputStream(file,true); //文件输出流将数据写入文件 outStream.write(sourceByte); String huanhang = System.getProperty("line.separator"); outStream.write(huanhang.getBytes()); } catch (Exception e) { e.printStackTrace(); // do something }finally { try { outStream.close(); }catch (Exception e){ e.printStackTrace(); } } } }
调用:
Date day=new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); FileUtils.logWrite(String.format("TableName:%s, 共有%s条数据." , "中国", 99) , "/runlog/"+ df.format(day) + "/" + "999" +".txt");
分类:
JAVA
, IntelliJ IDEA
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
2021-01-28 Windows下部署jar包启动服务后,过段时间出现服务假死、暂停或无反应现象
2010-01-28 Aptana :调试 javascript 图解