如何两次或多次读取BufferedReader?

时间:2013-06-24 06:36:19

标签: java io inputstream bufferedreader

我有一个文本文件,每行一个整数 -

10
20
50

我想阅读并打印这些数字两次甚至多次。我尝试了一些代码而失败了。如何更改代码以打印列表两次?

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

5 个答案:

答案 0 :(得分:5)

现在您可以多次打印。

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);
}

答案 1 :(得分:3)

您可以使用mark()reset(),也可以再次打开该文件。根据您的使用情况,您可能希望将数据存储在内存中吗?

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

答案 2 :(得分:1)

还有另一种可能性:将听众附加到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();
        }
    }
}

这样使用:

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"));
    }

因此文件被读取一次,它可以处理大文件,因为文件的内容不在内存中。

答案 3 :(得分:1)

 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

答案 4 :(得分:0)

多次读同样的东西会导致表现不佳...... 除非您有充分的理由进行多次读取,否则请在main方法的开头阅读,并将数据存储在适合您要求的数据结构中。