Import Java.io.BufferedWriter;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStreamWriter;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
public class Loginfo {
private static String filepath = "c:\\log\\";
private static Calendar C = Calendar.getinstance ();
Private static final SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
/**
* Log File
* @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 ("User" +username+, IP "+ip+", "+dowhat+" Operation \ n ");
Bw.flush ();
catch (FileNotFoundException e) {
SYSTEM.OUT.PRINTLN ("File not Found");
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
finally{
if (NULL!=BW) {
try {
Bw.close ();
catch (IOException e) {
SYSTEM.OUT.PRINTLN ("Flow operation exception");
}
}
}
}
/**
* Create log files based on time
* @return
*/
public static File CreateFile () {
Create a log file every day, named "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 ("Path does not exist");
}
return file;
}
public static void Main (string[] args) {
File File = CreateFile ();
Savelog (file, "WindForce", "192.168.0.1", "Change Password");
}
}