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

Posted on 2009-02-21
Medium Priority
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
  • 2
LVL 86

Accepted Solution

CEHJ earned 1500 total points
ID: 23702478
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

ID: 23702580
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

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

Expert Comment

ID: 23702593
thats incorrect, it is inefficient.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

850 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