How to read a BufferedReader twice or multiple times?

32,272

Solution 1

Now you can print multiple times.

BufferedReader br = new BufferedReader(new FileReader( "D:/log_2071-04-31.txt" ));
String strLine;
ArrayList<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);
}

Solution 2

You can use mark() and reset() or just open the file again. Depending on your use, perhaps you want to store the data in memory?

Reference - http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html#reset%28%29

Solution 3

There is another possibility: Attach listeners to a MultiReader.

interface Listener {
    void read(String line);
}

class MultiReader {
    final List<Listener> listeners = new ArrayList<Listener>();

    public void addListener(Listener l) {
        this.listeners.add(l);
    }

    public void read(File file) throws IOException {
        final BufferedReader br = new BufferedReader(new FileReader(file));
        try {
        String line;
            while ((line = br.readLine()) != null) {
                for (Listener l : this.listeners) {
                    l.read(line);
                }
            }
        } finally {
            br.close();
        }
    }
}

to be used like this:

public class MultiBufferedReader {
    public static void main(String[] args) throws IOException {
        MultiReader mr = new MultiReader();

        mr.addListener(new Listener() {
            @Override
            public void read(String line) {
                System.out.println("1: Got " + line);
            }
        });

        mr.addListener(new Listener() {
            @Override
            public void read(String line) {
                System.out.println("2: Got " + line);
            }
        });

        mr.read(new File("/etc/hosts"));
    }

So the file is read once, and it can handle large files, because the file's content is not held in memory.

Solution 4

 import java.io.BufferedReader;
 import java.io.DataInputStream;
 import java.io.FileInputStream;
 import java.io.InputStreamReader;

 public class DoubleBuffer {

public static void main(String[] args) {

    try {

        FileInputStream fstream = new FileInputStream("c:/files/numbers.txt");

        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;

            // Read rows
            while ((strLine = br.readLine()) != null) {
                System.out.println(strLine);
            }   
            // return stream to top of file
            fstream.getChannel().position(0);
            br=new BufferedReader(new InputStreamReader(fstream));


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

        in.close();
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }//try-catch

}// main

 }// class
Share:
32,272
Use your head
Author by

Use your head

You're supposed to use your brains, not spill em.

Updated on September 16, 2020

Comments

  • Use your head
    Use your head over 3 years

    I have a text file with one integer per row -

    10
    20
    50
    

    I want to read and print these numbers twice or maybe even multiple times. I tried some code and it failed. How do I change my code to print the list twice ?

    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    
    public class DoubleBuffer {
    
        public static void main(String[] args) {
    
            try {
    
                FileInputStream fstream = new FileInputStream("c:/files/numbers.txt");
    
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                String strLine;
    
                    // Read rows
                    while ((strLine = br.readLine()) != null) {
                        System.out.println(strLine);
                    }   
    
                    // Read rows again
                    while ((strLine = br.readLine()) != null) {
                        System.out.println(strLine);
                    }
    
                in.close();
            } catch (Exception e) {
                System.err.println("Error: " + e.getMessage());
            }//try-catch
    
        }// main
    
    }// class