• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Looping through BufferedReader Class

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
}
}
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic