Reading from BufferedReader more than once in Java

I have the following code snippet in Java:

HttpURLConnection con = (HttpURLConnection)new URL(url).openConnection();
con.connect();
InputStream stream = con.getInputStream();
BufferedReader file = new BufferedReader(new InputStreamReader(stream));

At this point, I read the file from start to finish, looking for something:

while (true)
{
    String line = file.readLine();
    if (line == null)
        break;
    // Search for something...
}

Now I want to find something else in the file without opening another URL connection .

For reasons unrelated to this issue, I want to avoid finding both things "in the same file scan."

Questions:

  • Can I rewind a file with reset?

  • If so, should it be applied to an object InputStream, to an object, BufferedReaderor both?

  • If not, should you just close the file and open it again?

  • If so, should it be applied to an object InputStream, to an object, BufferedReaderor both?

  • If not, how else can I scroll through the file again without having to read the URL connection again?

+4
3

reset(), , mark() ', . , .. BufferedReader.

, , , ( , ) .

+3

:

  • mark
  • skip
  • reset

, markSupported() true. , , intput, markSupported() , false , .

, , URL: , reset , . , , .

+1

- InputStreamSource, . , . , "" .

Edit: Also found guavas ByteSource and CharSource that have the same purpose.

0
source

All Articles