6

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

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
4

5 回答 5

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);
}
于 2013-06-24T06:47:41.327 回答
3

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

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

于 2013-06-24T06:38:00.833 回答
2

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

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

于 2013-06-24T07:57:57.797 回答
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
于 2016-01-01T18:25:46.997 回答
0

多次读取相同的内容会导致性能下降......除非您有非常充分的理由进行多次读取,否则请在 main 方法的开头读取并将数据存储在适合您要求的数据结构中。

于 2013-06-24T07:15:12.127 回答