I have the following piece of code 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 end while searching for something:
while (true)
{
String line = file.readLine();
if (line == null)
break;
// Search for something...
}
Now I want to search for something else within the file, without opening another URL connection.
For reasons unrelated to this question, I wish to avoid searching for both things "in a single file-sweep".
Can I rewind the file with reset
?
If yes, should I apply it on the InputStream
object, on the BufferedReader
object or on both?
If no, then should I simply close the file and reopen it?
If yes, should I apply it on the InputStream
object, on the BufferedReader
object or on both?
If no, how else can I sweep the file again, without reading through the URL connection again?
You can rewind the file with reset()
, provided that you have mark()
'ed the position you want to rewind to. These methods should be invoked on the decorator, i.e. BufferedReader
.
However, you may probably want to reconsider your design as you can easily read the whole file into some data structure (even a list of strings, or some stream backed by a string) and use the data multiple times.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With