首页 > 学院 > 开发设计 > 正文

日志输出

2019-11-15 00:49:55
字体:
来源:转载
供稿:网友
日志输出

做了个东西,突然发现把syso输出的东西在程序运行时同步输出到一个文件夹里不久是日志么,可是基础太差,好好的研究了一阵IO才把这个东西写出来

 1     PRivate static void log() throws IOException { 2         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//这是为了日志文件起名做的准备   3         SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");//这个是为了在日志中精确记录操作时间做的准备 4         Date date = new Date(); 5         File f = new File(".//log");//用的是相对路径,并且自己创建文件夹 6         if (!f.exists() && !f.isDirectory()) {//判断log文件夹存在并且他是一个文件夹 7             f.mkdir();//如果不存在,创建该文件夹 8         } 9         f = new File(".//log//" + format.format(date)+".txt");//创建对应文件10         Writer writer = new FileWriter(f,true);//输出string就用writer了11         writer.write(format2.format(date)+ "————日志结束————");12         writer.flush();13         writer.close();14     }

这里可以把这个方法改为传参的,然后只需将包含这个方法的类实例化并调用,调用时将syso想输出的语句传过来就可以记录日志了


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表