I'm trying to create a daily log file of users logged in to a web app. (for now, I'm taking a comman line argument) I can't seem to loop through a BufferedReader. I keep rewrting over the first line of the log file.
It feels like I'm overlooking something simple, should the BufferedReader be saved off to
String[]?
On a seperate note, is the
Java Logging API better suited for these purposes?
thanks in advance?
Andrew
import java.util.*;
import java.text.*;
import java.io.*;
public class CreateLog {
public static void main(String[] args) {
try{
Date today = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String strToday = dateFormat.format(today);
String strPath = "C:/LocalRSB/logs";
File myDir = new File(strPath);
File myFile = new File(myDir, strToday + ".txt");
String[] strFiles = myDir.list();
String strRead = "";
FileWriter file = new FileWriter(myFile);
PrintWriter logIn = new PrintWriter(file);
for (int i=0; i < strFiles.length; i++){
if (!myFile.exists()) {
myFile.createNewFile();
}
}
FileReader readFile = new FileReader(myFile);
BufferedReader bufferFile = new BufferedReader(readFile);
while(true){
strRead = bufferFile.readLine();
if (strRead == null){
logIn.print(args[0]);
readFile.close();
logIn.close();
System.exit(0);
}
logIn.println( "\r\n" + strRead);
}
}
catch(IOException e){
System.out.println(e.toString());
}
}
}