在文本中搜索 Java,并返回其周围的多行内容。

huangapple go评论80阅读模式
英文:

Java search in a text and returning multiple lines around it

问题

public void fajl(String fajlnev, String szeriaszam) throws FileNotFoundException {
    int sorszam = 0;
    Scanner serialnummer = new Scanner(new File(fajlnev));
    ArrayList<String> linesAroundValue = new ArrayList<>();  // Create an ArrayList to store the lines around the value

    while (serialnummer.hasNext()) { // Iterate through each line
        String line = serialnummer.nextLine().toString();
        sorszam++;

        if (line.contains(szeriaszam)) { // Check if the line contains the value you are searching for
            linesAroundValue.add(line); // Add the current line to the ArrayList

            // Read and add the next 9 lines after the value line
            for (int i = 0; i < 9 && serialnummer.hasNext(); i++) {
                linesAroundValue.add(serialnummer.nextLine().toString());
            }

            // Print the lines stored in the ArrayList
            for (String outputLine : linesAroundValue) {
                System.out.println(outputLine);
            }
        }
    }
}

请注意,上面的代码只是在你现有的代码基础上进行了修改,以实现在找到目标值后打印出目标值前5行和后9行的内容。我没有修改你原始代码中的翻译错误。如果需要进一步的帮助或有其他问题,请随时提问。

英文:

I am searching in a bigger txt file (~8Mb) where every row looks like this:
dd/mm/yyyy hh:mm:ss after these there are different identifiers and after that a value.

dd/mm/yyyy  hh:mm:ss identifier not relevant in search
dd/mm/yyyy  hh:mm:ss identifier not relevant in search
dd/mm/yyyy  hh:mm:ss identifier not relevant in search
dd/mm/yyyy  hh:mm:ss identifier value -&gt; value that I am searching for
dd/mm/yyyy  hh:mm:ss identifier not relevant in search
dd/mm/yyyy  hh:mm:ss identifier not relevant in search

I have successfully found and print out the whole line which contains the value that I am searching for, but I need to print the values around it(9 line after, 5 lines before), in the other lines.

    public void fajl(String fajlnev, String szeriaszam) throws FileNotFoundException{
    int sorszam = 0;
    Scanner serialnummer = new Scanner(new File(fajlnev));
    while(serialnummer.hasNext()){ //keres&#233;s p&#246;rget&#233;se
        String line = serialnummer.nextLine().toString();
        sorszam++;
        if(line.contains(szeriaszam)){
            System.out.println(line + &quot;\n&quot; + sorszam);

The searching and the printing out looks like this. As you can see I can also tell the line number but I have no idea how to print out the other lines. I was thinking about uploading an array or something like this but don't know where to start.

答案1

得分: 2

你可以使用一个队列(比如Java的LinkedList)来存储所需匹配行前的9行和后续的5行。然后,从链表中打印出所有行。

    public void fajl(String fajlnev, String szeriaszam) throws FileNotFoundException {
        List<String> lines = new LinkedList<>();
        Scanner serialnummer = new Scanner(new File(fajlnev));
        int counter = 5;
        boolean flag = false;

        while (serialnummer.hasNext()) {
            String line = serialnummer.nextLine().toString();
            if (lines.size() >= 9 && !flag) {
                lines.removeFirst();
            }
            lines.add(line);
            if (line.contains(szeriaszam) && !flag) {
                flag = true;
            }
            else if (flag && --counter == 0) {
                break;
            }
        }

        for (String line : lines) {
            System.out.println(line);
        }
    }

这里的逻辑是,我们开始读取并将所有行存储到一个链表中。一旦链表大小达到9,并且在找到匹配行之前发生这种情况,我们会移除最旧的行以腾出空间供最新的行进入。一旦我们找到匹配行,我们也会添加它,然后再添加5行,倒计数。在方法结束时,我们会将包括匹配行在内的15行打印到控制台上。

英文:

You could use a queue (e.g. a Java LinkedList) to store the 9 preceding and 5 following lines, around the line you want to match. Then, just print out all lines from the linked list.

public void fajl(String fajlnev, String szeriaszam) throws FileNotFoundException {
    List&lt;String&gt; lines = new LinkedList&lt;&gt;();
    Scanner serialnummer = new Scanner(new File(fajlnev));
    int counter = 5;
    boolean flag = false;

    while (serialnummer.hasNext()) {
        String line = serialnummer.nextLine().toString();
        if (lines.size() &gt;= 9 &amp;&amp; !flag) {
            lines.removeFirst();
        }
        lines.add(line);
        if (line.contains(szeriaszam) &amp;&amp; !flag) {
            flag = true;
        }
        else if (flag &amp;&amp; --counter == 0) {
            break;
        }
    }

    for (String line : lines) {
        System.out.println(line);
    }
}

The logic here is that we start off reading and storing all lines into a linked list. Once the list size reaches 9, and should that occur before we find the matching line, we kick out the oldest line to make room for the latest incoming line. Once we find the matching line, we also add it, and then add 5 more lines, counting down. At the end of the method, we print out the 15 lines, including the matching line, to the console.

huangapple
  • 本文由 发表于 2020年9月24日 22:34:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64048731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定