简体   繁体   English

Java 在文本中搜索并在文本周围返回多行

[英]Java search in a text and returning multiple lines around it

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.我在一个更大的 txt 文件(~8Mb)中搜索,其中每一行看起来像这样: dd/mm/yyyy hh:mm:ss 在这些之后有不同的标识符,然后是一个值。

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 -> 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.我已成功找到并打印出包含我正在搜索的值的整行,但我需要在其他行中打印它周围的值(之后 9 行,之前 5 行)。

    public void fajl(String fajlnev, String szeriaszam) throws FileNotFoundException{
    int sorszam = 0;
    Scanner serialnummer = new Scanner(new File(fajlnev));
    while(serialnummer.hasNext()){ //keresés pörgetése
        String line = serialnummer.nextLine().toString();
        sorszam++;
        if(line.contains(szeriaszam)){
            System.out.println(line + "\n" + 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.我正在考虑上传一个数组或类似的东西,但不知道从哪里开始。

You could use a queue (eg a Java LinkedList ) to store the 9 preceding and 5 following lines, around the line you want to match.您可以使用队列(例如 Java LinkedList )在要匹配的行周围存储前 9 行和后 5 行。 Then, just print out all lines from the linked list.然后,只需打印出链表中的所有行。

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);
    }
}

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.一旦列表大小达到 9,并且如果在我们找到匹配行之前发生这种情况,我们就会踢出最旧的行,为最新的传入行腾出空间。 Once we find the matching line, we also add it, and then add 5 more lines, counting down.一旦找到匹配的行,我们也添加它,然后再添加 5 行,倒计时。 At the end of the method, we print out the 15 lines, including the matching line, to the console.在方法结束时,我们将 15 行(包括匹配行)打印到控制台。

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

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