Reading from file

Heres the codez:publicvoid readWrite(){

try{

File file =new File("fileWrite2.txt");

PrintWriter fw =new PrintWriter(file);

BufferedWriter bf =new BufferedWriter(fw);

bf.write("line 1");

bf.newLine();

bf.write("line 2");

bf.newLine();

bf.write("line 3");

bf.newLine();

bf.flush();

bf.close();

FileReader fr =new FileReader(file);

BufferedReader br =new BufferedReader(fr);

do{

System.out.println(br.readLine());

}while (br.readLine() !=null);

}catch (Exception ioe){

System.out.println("IOException");

}

}

As you can see I write to a file then try to read it. Now how, I ask you, could I go through the file and print out each line ?

The output at the moment is as follows : line 1

line 3

line 2 is AWOL, I know the problem is somewhere in that while loop but how do loop through the lines of the file ?

[1701 byte] By [gtommoa] at [2007-11-27 11:04:37]
# 1

do {

System.out.println(br.readLine());

} while (br.readLine() != null

because, the above code, by you, reads a line and prints it, then reads a line, but does nothing withit, then reads a line and prints it, etc, etc, etc.

try as follows:

String line = null;

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

System.out.println(line);

}

masijade.a at 2007-7-29 13:01:49 > top of Java-index,Java Essentials,New To Java...
# 2

Many thanks

gtommoa at 2007-7-29 13:01:49 > top of Java-index,Java Essentials,New To Java...