【问题标题】:Read range of lines from Textfile从文本文件中读取行的范围
【发布时间】:2014-11-07 09:15:41
【问题描述】:

我有一个相当大的文本文件,如下所示:

0  239.0 -13.0 0
1  240.0 -13.0 33
2  241.0 -13.0 34
3  242.0 -13.0 44
4  243.0 -13.0 45
5  244.0 -13.0 74
6  245.0 -13.0 74
7  246.0 -13.0 79
8  247.0 -13.0 79
9  248.0 -13.0 113
10  249.0 -13.0 113
11  250.0 -13.0 120
12  251.0 -13.0 120
13  252.0 -13.0 153
14  253.0 -13.0 153
15  254.0 -13.0 160
16  255.0 -13.0 160
17  256.0 -13.0 194
18  257.0 -13.0 195
19  258.0 -13.0 199
20  259.0 -13.0 200
21  260.0 -13.0 232
22  261.0 -13.0 232
23  262.0 -13.0 239
...
...

每行的最后一个条目是timestamp。 现在我想实现一个名为read(int timestamp, int range) 的方法,它以一种有效的方式从这个文本文件中返回一系列行。 例如,如果我调用read(10250,100),我想查找时间戳为 10250 的行(如果存在,则取最近的下一个)并返回时间戳行 before 的 100 行,带有timstamp 本身和 100 行之后字符串数组中的时间戳行。

这是我当前对read(int timestamp, int range)的实现:

public static void read(int timestamp,int range) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader("LOG_TEST"));
    String line;
    int currTimestamp;
    while ((line = br.readLine()) != null) {
        currTimestamp = Integer.parseInt(line.split("\\s+")[3]);
        if (currTimestamp >= timestamp) {
            for(int i = 0; i<range;i++){
                System.out.println(br.readLine());
            }
            break;
        }
    }
    br.close();
}

问题是这个实现只打印出时间戳之后的 100 行。我不知道如何包含之前的 100 行。因为我不知道时间戳行在哪一行,所以我需要以某种方式阅读“backwards”以获取之前的 100 行。 我怎样才能以有效的方式实现这一点? 亲切的问候

【问题讨论】:

  • 文本文件是连续的...为什么不使用数据库来进行随机访问呢?
  • 在遍历所有行时,您可以将它们放入链表中。如果该列表已达到 100 个条目,您可以删除第一个并放入下一个元素。所以当你找到你的元素时,你总是得到 100 行。

标签: java android file bufferedreader


【解决方案1】:

我会使用固定大小的CircularFifoQueue range,例如100.引用它的add方法:

将给定元素添加到此队列。如果队列已满,则丢弃最近添加的元素,以便插入新元素。

这样,您只保留range 行,而不是之前的所有行。

你可以从here下载jar。

它还提供了一个get(int index) 方法,您可以使用该方法按您希望的顺序打印行,但我认为您不需要这样做,因为迭代器按插入顺序返回元素。所以,这是我的建议:

public static void read(int timestamp,int range) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader("LOG_TEST"));
    String line;
    CircularFifoQueue<String> prevLines = new CircularFifoQueue<>(range);
    int currTimestamp;
    while ((line = br.readLine()) != null) {            
        currTimestamp = Integer.parseInt(line.split("\\s+")[3]);
        if (currTimestamp >= timestamp) {
            for (String prevLine : prevLines) {  //prints the range previous lines
                System.out.println(prevLine);
            }
            System.out.println(line);            //prints the current line
            for(int i = 0; i<range;i++){         //prints the following range lines
                System.out.println(br.readLine());
            }                
            break;
        } else {
            prevLines.add(line);
        }
    }
    br.close();
}

【讨论】:

  • 谢谢@vefthym,这段代码“几乎”可以工作。要打印当前行,我必须在两个for 循环之间插入行System.out.println(line),并将第二个for 循环的&lt;= 符号更改为&lt;。
  • @user1291235 是的,你是对的!我更新了我的答案。很抱歉
【解决方案2】:

试试:

public static void read(int timeStamp,int range) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader("LOG_TEST"));
    String line;
    ArrayList<String> save=new ArrayList<String>();
    int currTimestamp;
    String str:
    while ((line = br.readLine()) != null) {
        timestamp = Integer.parseInt(line.split("\\s+")[3]);
        save.add(br.readLine());
        int i=save.length-100;
        if (currTimestamp >= timestamp) {
            //logic for print 100 line from array save
            ...

            for(int i = 0; i<range;i++){
                System.out.println(br.readLine());
            }
            break;
        }
    }
    br.close();
}

【讨论】:

  • 链表可能会更有效。
猜你喜欢
  • 1970-01-01
  • 2020-09-25
  • 2016-10-10
  • 2012-06-24
  • 1970-01-01
  • 2021-11-23
  • 2013-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多