Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: BufferedReader - Freezes on 2nd read

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    57
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default BufferedReader - Freezes on 2nd read

    Hello

    I am trying to read from a buffered reader.

    Cannot really post the code as it is for an assignment I have.

    Basically i need to send text to a buffered reader, read this into a file using printWriter but also display this on the screen.

    So when i do a while loop that loops until EOF is reached works fine.

    the 2nd while loop that reads into a file using the same method above just freezes the program.

    Do i have to open the close the bufferedReader when that read has finished ready for another read? or something as i am really struggling?

    using .readLine() method


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: BufferedReader - Freezes on 2nd read

    You cannot reuse a BufferedReader like this - it has reached the end of the file and unless you use a RandomAccessFile (or some other class which allows you to reset the pointer) the file marker will remain at the end of the file (eg no second read). Not sure why you need to read the file twice...but you can just recreate the BufferedReader (be sure to close the Readers when you are done with them).

  3. #3
    Member
    Join Date
    Nov 2009
    Posts
    57
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: BufferedReader - Freezes on 2nd read

    Thanks for the quick reply: here is some of my code:

    This display's it:
    public void display()
        {
            String line;
     
            try
            {
                line = from.readLine();
     
     
               while(!line.equals("EOF"))
               {
                   System.out.println(line);
                   line = from.readLine();
     
               }
     
             }
             catch(IOException e)
             {
                System.out.println("Error " + e.getMessage());
             }
     
        }

    This saves it:
    private void save()
        {
            try 
            {
                PrintWriter pwOutput = new PrintWriter(arg);
     
                    String line = from.readLine();
                    while(!line.equals("EOF"))
                    {
                        pwOutput.println(line);
                        line = from.readLine();
                    }
     
                pwOutput.flush();
                pwOutput.close();
            }
            catch (IOException e) 
            {
                System.out.println("Error " + e.getMessage());
            }
     
     
        }
    Last edited by mds1256; March 14th, 2011 at 02:51 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: BufferedReader - Freezes on 2nd read

    Like I said, don't reuse the BufferedReader, and I'd also recommend not looking for "EOF" - the readLine returns null....so check for the returned string to be null. I'd recommend rewriting your code to take these into account. Depending upon what you wish to accomplish, you could read the file in once, and then use the read contents for the multiple tasks.

  5. #5
    Member
    Join Date
    Nov 2009
    Posts
    57
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: BufferedReader - Freezes on 2nd read

    thanks for the reply. need to use EOF as that what the assignment says.

    Basically i need to send and receive text to a server / client. So i send to the server (server has bufferedReader to read all data sent from client) then the server replies back to client (client uses bufferedReader to read all data sent from the server)

Similar Threads

  1. BufferedReader
    By reg4ltip in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 20th, 2010, 12:15 PM
  2. BufferedReader error?
    By Umogrim in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 28th, 2010, 08:51 PM
  3. JSP freezes after AJAX call !!
    By java_freek in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: April 16th, 2010, 10:31 AM
  4. Scanner vs BufferedReader?
    By Bill_H in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: October 27th, 2009, 09:44 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM