简体   繁体   English

计算两行之间的行数

[英]Count no of lines between two lines

How to achieve the following.如何实现以下。 Here is a sample text file..这是一个示例文本文件..

AH
1
2
3
BH
21
BT03
CH
CT02
AT10

This is a sample text file.这是一个示例文本文件。 I need read each line and find the count.. For example, there are 10 Lines within A record.. And 3 lines within B record and two lines within C Record.我需要阅读每一行并找到计数。例如,A 记录中有 10 行。B 记录中有 3 行,C 记录中有两行。

How do I get this count.我如何得到这个计数。 H-Header and T-Trailer. H-Header 和 T-Trailer。 You may say, read the T record to find the count, but that count might be wrong.您可能会说,读取 T 记录以查找计数,但该计数可能是错误的。 Hence I am trying to find the count and update the trailer records correctly.因此,我试图找到计数并正确更新预告片记录。

I cannot upload the Java code I have written as I'm on mobile now.. Any suggestions is highly appreciated我现在在移动设备上无法上传我编写的 Java 代码.. 非常感谢任何建议

Thanks谢谢

I'm not sure if I understand your question, you just want to count lines in a file?我不确定我是否理解您的问题,您只想计算文件中的行数? Try something like this:尝试这样的事情:

Path path = Paths.get("yourFile.txt");
long lineCount = Files.lines(path).count();

use below code and read all file in array.使用下面的代码并读取数组中的所有文件。

String[] stringArray = new String[10];

    int length = stringArray.length;
    for (int i = 0; i < length; i++) {
        String stringI = stringArray[i];
        if (stringI.contains("H")) {
            // Head rec
            String headRecName = stringI.replace("H", "");
            int count = 1;
            for (int j = i + 1; j < length; j++) {
                count++;
                String stringJ = stringArray[j];
                if (stringJ.contains("T") && headRecName.equals(stringJ.replace("T", ""))) {
                    stringArray[j] = stringArray[j] + count;
                }

            }

        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM