Java - How to read a file twice using buffer reader or use stream twice

how to read inputstream twice in java
java clone inputstream
java inputstream multiple read
java use input stream twice
bufferedreader
java read from stream multiple times
java inputstream read file
inputstream mark

How to read file twice eihher using buffer reader or using stream twice ???

  • That I need manipulate large amounts of data in the code, so the performance needs to be considered.

Sample code 1 below, gives exception "stream closed" -

Url url = 'www.google.com'
InputStream in = url.openStream();
BufferReader br = new BufferReader(in);

Stream<String> ss = br.lines; // read all the lines

List ll = ss.collect();
br.close();
BufferReader br = new BufferReader(in); //exception occurs

Sample code 2 below, gives exception "stream closed/being used" -

Url url = 'www.google.com'
InputStream in = url.openStream();
BufferReader br = new BufferReader(in);

Supplier<Stream<String>> ss = br.lines; // read all the lines

List ll = ss.collect();
List xx = ss.collect();. // Exception occurs

Please ignore the syntax, it's just a draft code. Kindly suggest.

In terms of use a stream is somewhat analogous to an iterator in that it can only be used once.

If you want to use the contents of the same stream again you need to create a new stream as you did the first.

As of Java 12, you can pass values of the same stream into two branches by using the Collectors.teeing() method.

List.stream().collect(Collectors.teeing(
                Collector1, // do something with the stream
                Collector2, // do something else with the stream
                BiFunction, use to merge results)

You can also do this.

Supplier<Stream<String>> ss1 = br.lines; // read all the lines
Supplier<Stream<String>> ss2 = br.lines; // read all the lines

Now you can use ss1 and ss2 as two separate streams.

Reading data twice from input stream, If there're situations to use single input stream twice, we should be very careful FileInputStream fis = new FileInputStream("CopyMultipleFiles.java"); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); And we'll be reading the file and copying the bytes using copyBytes method. BufferedReader is a Java class to reads the text from an Input stream (like a file) by buffering characters that seamlessly reads characters, arrays or lines. In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream.

Here have an example below. You could use it to read as many times as you wish.

BufferedReader br = new BufferedReader(new FileReader( "users/desktop/xxx.txt" ));
String strLine;
List<String> ans= new ArrayList<String>();

// Read rows
while ((strLine = br.readLine()) != null) {
    System.out.println(strLine);
    ans.add(strLine);
}

// Read again
for (String result: ans) {
    System.out.println(result);
}

reference

https://www.dreamincode.net/forums/topic/272652-reading-from-same-file-twice/

How to read a java.io.BufferedReader twice, HI, I need to read a bufferedreader twice i.e I want to open it again and perform If I use BufferedReader, I have to close the file an reopen it again when I want to The Java API comes with many classes to allow you to read and write streams � hi I am very new to java and I would like to know how to write a program that would move multiple files from a different folder, delete the files from the previous folder and copy and rename those files in another folder

You cannot. A stream is just like its real-life watery counterpart. You can observe the water going under the bridge you're standing on, but you can't instruct the water to go back to the top of the hill so that you can observe it again.

Either have each consumers process each line before moving on to the next line, or if that is not possible then you will need to create your own "buffer" of the entire thing: i.e. store each line to Collection<String>, which the second (and third, and fourth...) consumer can iterate over. The potential problem with this is that it's a bigger memory overhead. The HTML of most websites is not likely to prove to be much of a problem in this regard.

Your last example can be trivially fixed by copying the list.

List ll = ss.collect();
List xx = new ArrayList(ll);

Reading a File Line by Line in Java, Being able to read a file line by line gives us the ability to seek only the relevant try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; while If you're already familiar with the Java 8 Streams, you can use them as a amount of time, and you should think twice before using it on large text files. BufferedReader(Reader in, int sz) : Creates a buffering character-input stream that uses an input buffer of the specified size. Methods: void close() : Closes the stream and releases any system resources associated with it.Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException.

Read stream twice, Read stream twice - java. One for reading into existing buffer (defintion is analogous to calling public int read(byte b[], int off, int len) of InputStream class). Finally, if your need is to read image, then use : BufferedImage image = ImageIO.read(new You can read in an image from a file with just a single line of code:� Stream.Reader.Peek() method checks if there are more characters are left to read in a file. The method returns an integer. If the value is less than 0, there are no more characters left to read. The following code snippet uses the Peak() method to check if there are more characters left to read in a file.

How to read a text file in Java, The advantage of using a BufferedReader to read a text file is speed. data in its internal buffer which you can read it without accessing file multiple times. by line in Java e.g. you can use Files.lines() method to get all lines as Stream in Java� Here is two example programs that shows file read and write using buffered streams. File copy using Buffered streams. This program copies the file named file1.txt to another file named file1_copy.txt using BufferedInputStream and BufferedOutputStream classes. The program creates a buffered input stream with the source file and a buffered output

How to use reset method in java.io.BufferedReader, Best Java code snippets using java.io.BufferedReader.reset (Showing top 20 results out of 1,485). Common ways to File file;new BufferedReader(new FileReader(file)); Smart code suggestions by Codota. } Resets the stream to the most recent mark. Whether or not any more data can be read from this data source. Java BufferedReader Class for beginners and professionals with examples on Java IO or Input Output in Java with input stream, output stream, reader and writer class. The java.io package provides api to reading and writing data.

Comments
  • Why you want to read the stream two times? store the result and work on it
  • maybe write the code in a method and call it twice, this way it will connect to the url twice?
  • I have a large data file. So, Storing the content of file to an object uses up more heap memory. I need to perform two operation using the file. One is to read the file completely and next is to get the total number of lines in that file.
  • Thanks. Ideally it works fine for less volume of data(less no. Of lines). For handling very high volume like 150k, list size (heap) won't be sufficient and more over doing it in loop will affect the performance.
  • Thanks. It's a simple and effective answer. But still for more no. Of lines (150k+) heap size will be a trouble.