/* 建立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");