Is there a way to set the reading position of a BufferedReader to the first line?

Posted on 2009-02-21
Last Modified: 2013-11-23
When reading the contents of an URL using a BufferedReader as shown below, how does one reset the reader so that it is at line when finished reading the document?

URL url = new URL(urlString);
BufferedReader urlReader = new BufferedReader(new InputStreamReader(url.openStream()));

Or might I as well just reinitialize the reader, I would think that reinitializing was an inefficient (ressource costly) way to do it?:

BufferedReader urlReader = new BufferedReader(new InputStreamReader(url.openStream()));
Question by:Suppai
    LVL 86

    Accepted Solution

    It's not really inefficient, but you could use mark and reset. Since the mark point could be difficult to determine though, it's better to just read it again.
    LVL 92

    Expert Comment

    you need to recreate the read to achieve that and yes it is inefficient. It requires you to download the page all over again.
    You might want to think about downloading the page and store a copy of the bytes locally if you are going to be wanting to read it again.

    Heres an example of one way to store the contents

    Exactly how you implement it depends on your requirements


    Author Closing Comment

    Ok Ill just read again, the mark and reset system seemed to be a bit clumsy.
    LVL 92

    Expert Comment

    thats incorrect, it is inefficient.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    method argument as final 1 54
    Convert BufferedReader to File 1 49
    scores100 challenge 3 67
    endX challenge 2 31
    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now