Can't read a text file using java

Hi All

I am trying to read a log file.

ProgramA keeps updating the log file, while my program ProgramB reads it.

For some readson my ProgramB is not able to read the last two lines in the log file. If I run the program in debug mode it is reading all the lines.

This is having me frustrated.

Please let me know if there is a way to read entire contents.

Here is how I am reading the files ( 2ways)

/*

private static String readFileAsString(String filePath)

throws java.io.IOException{

StringBuffer fileData = new StringBuffer(1000);

FileReader fr = new FileReader(filePath);

BufferedReader reader = new BufferedReader(fr);

char[] buf = new char[1024];

int numRead=0;

while((numRead=reader.read(buf)) != -1){

String readData = String.valueOf(buf, 0, numRead);

fileData.append(readData);

buf = new char[1024];

}

reader.close();

fr.close();

return fileData.toString();

}

*/

/**

* Fetch the entire contents of a text file, and return it in a String.

* This style of implementation does not throw Exceptions to the caller.

*

* @param aFile is a file which already exists and can be read.

*/

staticpublic String readFileAsString(String filePath){

//...checks on aFile are elided

StringBuffer contents =new StringBuffer();

//declared here only to make visible to finally clause

BufferedReader input =null;

try{

//use buffering, reading one line at a time

//FileReader always assumes default encoding is OK!

input =new BufferedReader(new FileReader(filePath) );

String line =null;//not declared within while loop

/*

* readLine is a bit quirky :

* it returns the content of a line MINUS the newline.

* it returns null only for the END of the stream.

* it returns an empty String if two newlines appear in a row.

*/

while (( line = input.readLine()) !=null){

contents.append(line);

contents.append(System.getProperty("line.separator"));

}

}

catch (FileNotFoundException ex){

ex.printStackTrace();

}

catch (IOException ex){

ex.printStackTrace();

}

finally{

try{

if (input!=null){

//flush and close both "input" and its underlying FileReader

input.close();

}

}

catch (IOException ex){

ex.printStackTrace();

}

}

return contents.toString();

}

[3946 byte] By [bib1a] at [2007-11-27 2:54:51]
# 1
stop cross posting !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
java_2006a at 2007-7-12 3:31:00 > top of Java-index,Java Essentials,New To Java...
# 2

If you're using JDK 5 or later, you could use the Scanner class for input information. See below:

try {

Scanner reader = new Scanner(new File("C:\\boot.ini"));

StringBuilder sb = new StringBuilder();

while (reader.hasNextLine()) {

sb.append(reader.nextLine());

sb.append(System.getProperty("line.separator"));

}

System.out.println(sb.toString());

} catch (FileNotFoundException e) {

e.printStackTrace();

}

nofearinca at 2007-7-12 3:31:00 > top of Java-index,Java Essentials,New To Java...
# 3

> ProgramA keeps updating the log file, while my program ProgramB

> reads it.

>

> For some readson my ProgramB is not able to read the last two lines

> in the log file. If I run the program in debug mode it is reading all the

> lines.

> This is having me frustrated.

Just a guess:

Could it be that ProgramB reaches the end of the file before ProgramA writes the two last lines ?

And the fact that it is run in debug mode gives enough time for ProgramA to finish before ?

TimTheEnchantora at 2007-7-12 3:31:00 > top of Java-index,Java Essentials,New To Java...
# 4
> stop cross posting !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Is this cross posting ?Would you mind providing link(s) to the other post(s) ?
TimTheEnchantora at 2007-7-12 3:31:00 > top of Java-index,Java Essentials,New To Java...
# 5

Thanks for the replies.

The problem I found out was, the program which writes log file is holding on to it, while my program is reading.

I tried to open using notepad (It opens fine), wordpad gives sharing violation.

I am confused as how java works.......

1. If ProgramA which writes the log file holds on to it. Why is Wordpad giving sharing violation. Why notepad can open full contents. Where as java program (ProgramB) can read only first 10 lines not the entire 12 lines.

2. How can I make ProgramB read all the contents like notepad does.

Please let me know.

bib1a at 2007-7-12 3:31:00 > top of Java-index,Java Essentials,New To Java...
# 6

Can you confirm that ProgramA has finished writing the 12 lines when ProgramB tries to read them ?

It still might simply be a timing/synchronization issue (+buffering?). What is the result if you (re)run ProgramB afterwards, is it able to read the whole file then ?

I don't think it is related to sharing violation, as ProgramB only reads the file (and doesn't throw any corresponding exception.)

TimTheEnchantora at 2007-7-12 3:31:00 > top of Java-index,Java Essentials,New To Java...
# 7
Yes, It can read the whole log file when ProgramB runs after ProgramA terminated.
bib1a at 2007-7-12 3:31:01 > top of Java-index,Java Essentials,New To Java...
# 8
Yes I can see in Notepad all the 12 lines. When I print out using my java program (ProgramB) it prints only 10 lines.
bib1a at 2007-7-12 3:31:01 > top of Java-index,Java Essentials,New To Java...