Java BufferedReader 在循环之前检查循环的下一行

发布于 2025-01-03 21:34:18 字数 597 浏览 0 评论 0 原文

我正在解析 .cvs 文件。 对于 cvs 的每一行,我使用解析的值创建一个对象,并将它们放入一个集合中。

在将对象放入地图并循环到下一个之前,我需要检查下一个 cvs 行是否与实际对象相同,但特定属性值不同。

为此,我需要检查缓冲区的下一行,但将循环缓冲区保持在同一位置。

例如:

BufferedReader input  = new BufferedReader(new InputStreamReader(new FileInputStream(file),"ISO-8859-1"));
String line = null;

while ((line = input.readLine()) != null) {
    do something

    while ((nextline = input.readLine()) != null) { //now I have to check the next lines
       //I do something with the next lines. and then break.
    }
    do something else and continue the first loop.
}

I'm parsing a .cvs file.
For each line of the cvs, I create an object with the parsed values, and put them into a set.

Before putting the object in the map and looping to the next, I need to check if the next cvs's line is the same object as the actual, but with a particular property value different.

For that, I need check the next lines of the buffer, but keep the loop's buffer in the same position.

For example:

BufferedReader input  = new BufferedReader(new InputStreamReader(new FileInputStream(file),"ISO-8859-1"));
String line = null;

while ((line = input.readLine()) != null) {
    do something

    while ((nextline = input.readLine()) != null) { //now I have to check the next lines
       //I do something with the next lines. and then break.
    }
    do something else and continue the first loop.
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老娘不死你永远是小三 2025-01-10 21:34:19
  1. 您可以使用 BufferedReader.mark(int)。要返回到调用 BufferedReader.reset() 的位置。 mark 的参数是“预读限制”;如果您在读取超过限制后尝试reset(),您可能会收到 IOException。

  2. 或者您可以使用RandomAccessFile改为:

    //获取当前位置
    长 pos = raf.getFilePointer();
    // 读取更多行...
    // 返回旧位置
    raf.seek(pos);
    

  3. 或者您可以使用 PushbackReader 允许您取消读取个字符。但有一个缺点: PushbackReader 不提供 readLine 方法。

  1. You can mark the current position using BufferedReader.mark(int). To return to the position you call BufferedReader.reset(). The parameter to mark is the "read ahead limit"; if you try to reset() after reading more than the limit you may get an IOException.

  2. Or you could use RandomAccessFile instead:

    // Get current position
    long pos = raf.getFilePointer();
    // read more lines...
    // Return to old position
    raf.seek(pos);
    
  3. Or you could use PushbackReader which allows you to unread characters. But there's the drawback: PushbackReader does not provide a readLine method.

守不住的情 2025-01-10 21:34:19

您还可以使用嵌套的 do...while 循环来执行此操作,而无需重新读取流中的任何内容:

public void process(File file) throws Exception {
    BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));
    String batchParent = input.readLine(); // read the first line in the file, this is a new batch
    String batchChild;
    do {
        currentBatch = new Batch(batchParent);
        do {
            batchChild = input.readLine();
        } while (addToBatchIfKeysMatch(batchParent, batchChild));
        // if we break out of the inner loop, that means batchChild is a new parent
        // assign it to batchParent and continue the outer loop
        batchParent = batchChild;
    } while (batchParent != null);
}

private boolean addToBatchIfKeysMatch(final String batchParent, final String batchChild) {
    if (batchChild != null && keysMatch(batchParent, batchChild)) {
        currentBatch.add(batchChild);
        return true;
    } else {
        return false;
    }
}

关键是只读取每个文件并将其作为内部循环中的子行处理,或者将parentLine 设置为新读取的值并继续外循环。

You can also do this with nested do…while loops, without re-reading anything from your stream:

public void process(File file) throws Exception {
    BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));
    String batchParent = input.readLine(); // read the first line in the file, this is a new batch
    String batchChild;
    do {
        currentBatch = new Batch(batchParent);
        do {
            batchChild = input.readLine();
        } while (addToBatchIfKeysMatch(batchParent, batchChild));
        // if we break out of the inner loop, that means batchChild is a new parent
        // assign it to batchParent and continue the outer loop
        batchParent = batchChild;
    } while (batchParent != null);
}

private boolean addToBatchIfKeysMatch(final String batchParent, final String batchChild) {
    if (batchChild != null && keysMatch(batchParent, batchChild)) {
        currentBatch.add(batchChild);
        return true;
    } else {
        return false;
    }
}

The key is to just read each file and either process it as a child line in the inner loop, or set the parentLine to the newly read value and continue the outer loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文