Use the same getInputStream () for 2 different bufferedReader

3

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 only one. My doubt is, I can simply do:

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

where

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

Or do I need to make a new HttpURLConnection for this?

    
asked by anonymous 02.12.2015 / 18:39

1 answer

2

It does not make 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 continue from where in has stopped.

This is because classes like BufferedReader and InputStreamReader are only adding behavior to InputStream , but the object remains the same.

You need to create two InputStream s. Or you can even InputStream remove the two information you need.

    
31.07.2016 / 15:17