• Post Reply Bookmark Topic Watch Topic
  • New Topic

Looping through BufferedReader Class  RSS feed

 
Andrew Babaian
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
}
}
}
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

overwrites existing data. You want to use:

The code you've written to iterate through a Reader until it hits EOF then write a line will not work because there's no association between a Reader and a Writer.
 
Andrew Babaian
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. Are you saying I'm missing an association or that I'm not supposed to have an assocation between a Reader and writer

Andrew
 
Andrew Babaian
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andrew Babaian:
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);
}

//this line is not needed
logIn.println( "\r\n" + strRead);


I see, the last println is not needed.

Andrew
 
Andrew Babaian
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My class and method seem to be working, but in the Tomcat server window, The IOException is thrown saying "Stream closed"

This exception was not thrown when I was passing a command line argument.

I packaged the class in web-inf\classes\beans dir and imported beans.* in the jsp page. Called CreateLog.LogIn(strLoginInfo) and the excepttion is thrown. Why is this happening now?
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andrew Babaian:
thanks. Are you saying I'm missing an association or that I'm not supposed to have an assocation between a Reader and writer
Andrew


The writer knows nothing about the reader. You don't need a file reader. By opening the file writer in append mode anything you write goes to the end of the file.
As for what's going on with your CreateLog.LogIn(strLoginInfo) method, sounds like you are invoking close() with one call then expecting the stream to be open in the next.
BTW, the Java Tutorial has a chapter on using IO Streams. It would be worth a look.
 
Andrew Babaian
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow, that helps. I just removed the entire Reading section and this works fine. I thought you need to read to do an append, but the boolean argument takes care of that.

thanks for the link to the tutorial, I'll check it out.

Andrew
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!