This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Priting contents in BuferedReader
Posted by Divya on January 30, 2002 at 5:09 PM
Hi, Try this code - I think this should solve your problem import java.io.*; public class JavaForum { public static void main(String args[]) { String line = null; StringBuffer concatStr = new StringBuffer(); try { BufferedReader fr = new BufferedReader(new FileReader("dat.txt")); while((line = fr.readLine()) != null) { concatStr.append(line + " "); } System.out.println("File Contents are-->" + concatStr.toString()); } catch(IOException e) { System.out.println(e.getMessage()); } } } cheers, Divya > Hey guys, > I am using a BufferedReader to read all the contents in a file. > It does it perfectly. However after reading the file, the program > needs to print all the contents it has read. The only way I am > allowed to do it is AFTER the program has finished reading the file. > Please Help me with this. Here is my code for reading the file. > // CODE > BufferedReader fr = new BufferedReader(new FileReader(nextLine)); > > //----------------------first element in this file-------------------------- > nextLine = fr.readLine(); > > //----------------------------second element in the file------------------------------ > nextLine = fr.readLine(); > > for(;;){ > nextLine = fr.readLine(); > > if(nextLine == null) > break; > } > > // System.out.println(fr.toString()); (this doesn't work) > > thanks
Replies:
|