java – Use the same getInputStream() for 2 different bufferedReader


I need to access a file on an HTTP server and retrieve information from 2 different sites within that file. At this moment I can withdraw from just one. My question is, I can simply do:

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
BufferedReader in2 = new BufferedReader(new InputStreamReader(con.getInputStream()));

being that

HttpURLConnection con = (HttpURLConnection) obj.openConnection();

or do i need to make a new HttpURLConnection for this?


It makes no sense to create two BufferedReader s pointing to the same InputStream . In practice they will be working on the same thing. If you try to read a file using BufferedReader in and then try to read the same file using BufferedReader in2 , in2 will in2 where in left off.

That's because classes like BufferedReader and InputStreamReader are just adding behavior to the InputStream , but the object remains the same.

You need to create two InputStream s. Or you can on the same InputStream pull out the two pieces of information you need.

Scroll to Top