第一次上传代码求指教,简单的操作日志记录

代码片段(1)[全屏查看所有代码]

1.[代码][Java]代码跳至package com.xjj.logManager;/** * Created by wangcx * on 2014/11/11. */import java.io.*;import java.text.SimpleDateFormat;import java.util.Calendar;public class LogManager {private static String filepath = “C:\\log\\”;private static Calendar c = Calendar.getInstance();private static final SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);/*** 记录日志文件** @param file* @param username* @param ip* @param dowhat*/public static void saveLog(File file, String username, String ip, String dowhat) {BufferedWriter bw = null;try {bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));bw.write(“用户 ” + username + “,ip为 ” + ip + “,进行了 ” + dowhat + ” 操作\n”);bw.flush();} catch (FileNotFoundException e) {System.out.println(“文件未找到”);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (null != bw) {try {bw.close();} catch (IOException e) {System.out.println(“流操作异常”);}}}}/*** 根据时间创建日志文件** @return*/public static File createFile() {//每天创建一个日志文件,名称为”2010-11-30.txt”File file = null;try {file = new File(filepath + sdf.format(c.getTime()) + “.txt”);if (!file.exists()) {file.createNewFile();}} catch (Exception e) {System.out.println(“路径不存在”);}return file;}public static void main(String[] args) {File file = createFile();saveLog(file, “windforce”, “192.168.0.1”, “更改密码”);}}

,才能做到人在旅途,感悟人生,享受人生。

第一次上传代码求指教,简单的操作日志记录

相关文章:

你感兴趣的文章:

标签云: