繁体   English   中英

在文本文件中搜索字符串

[英]Search string within a text file

我正在尝试在文本文件中搜索一组关键字。

我将它们作为字符串放置,并且if语句出现问题。

在这行上:

if(s.contains (keywords)) {

我以前没有 它说要执行以下操作:

字符串类型中的方法contains(CharSequence)不适用于参数(String []))

但这只是将String更改为CharSequence ,仍然导致错误!

这是代码:

import java.io.*;

public class SearchTextFile {

    public static void main(String args[]) throws Exception {
        int tokencount;
        FileReader fr = new FileReader("c:\\searchtxt.txt");
        BufferedReader br = new BufferedReader(fr);
        String s;
        int linecount = 0;

        String[] keywords = {
            "AB", "BC", "D1", "B11", "BL:", "B/1:", "B1L:", "B11:"
        };
        String line;

        while ((s = br.readLine()) != null) {
            if (s.contains(keywords)) {
                System.out.println(s);
                String nextLine = br.readLine();
                System.out.println(nextLine);
            }
        }
    }
}

由于String没有方法String.contains(String) ,因此可以按以下方式实现它:

将关键字数组更改为ArrayList<String> 然后,当您读一行时,使用String.split()方法获取数组中的所有单词。 现在,您可以遍历单词数组(通过从文件中读取行来创建),并检查keywordList是否包含单词。 注意:如果s与关键字完全相同,则KeywordList.contains(s)将为true 如果s是包含其他单词的字符串,但它包含keywords array中的一个或多个元素,则它将生成false s因此,此测试将不会产生有效的搜索结果。 搜索的目的是检查任何输入行s是否具有keywords数组中的至少一个keywords 因此,一种解决方案如下:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

public class SearchTextFile
{
    public static void main(String args[]) throws Exception
    {
        int tokencount;
        FileReader fr = new FileReader("c:\\searchtxt.txt");
        BufferedReader br = new BufferedReader(fr);

        String s = "";
        int linecount = 0;
        ArrayList<String> keywordList = new ArrayList<String>(Arrays.asList("AB", "BC", "D1", "B11", "BL:", "B/1:", "B1L:", "B11:"));

        String line;
        while ((s = br.readLine()) != null)
        {
            String[] lineWordList = s.split(" ");
            for (String word : lineWordList)
            {
                if (keywordList.contains(word))
                {
                    System.out.println(s);
                    String nextLine = br.readLine();
                    System.out.println(nextLine);
                    break;
                }
            }
        }
    }
}

一种更有效的方法是将关键字列表存储在集合中 ,还将标记列表存储在集合中的每一行上。 这样可以避免遍历列表中所有元素的效率低下。 考虑到这一点,我对您的代码进行了一些修改:

public static void main(String args[]) throws Exception
{
    int tokencount;
    FileReader fr=new FileReader("c:\\searchtxt.txt");
    BufferedReader br=new BufferedReader(fr);
    String s;
    int linecount=0;

    //String[]  keywords = { "AB", "BC","D1", "B11", "BL:", "B/1:","B1L:", "B11:"};
    Set<String> keywords = new HashSet<>(Arrays.asList("AB", "BC", "D1", "B11", "BL:", "B/1:", "B1L:", "B11:"));
    String line;
    Set<String> lineSet;

    while ((s=br.readLine())!=null) {
        lineSet = new HashSet(Arrays.asList(s.split(" ")));
        if(!Collections.disjoint(lineSet, keywords)) { //returns true if both sets have no elements in common;
            System.out.println(s);
            String nextLine = br.readLine();
            System.out.println(nextLine);
        }
    }
}

您不能将String数组( String[] )与contains使用,而应使用正则表达式来代替,即:

import java.util.regex.*;

    if (subjectString.matches("AB|BCD1|B11|BL:|B/1:|B1L:|B11:")) {
        System.out.println(s);
        String nextLine = br.readLine();
        System.out.println(nextLine);
    }

没有contains(String [])这样的方法。 正确的是contains(String)

就像@Pedro Lobito所说的,您可以使用正则表达式。 您也可以遍历字符串数组,

while ((s=br.readLine())!=null) {
    for (String str: keywords) {           
        if(s.contains (str)) {
            System.out.println(s);
            String nextLine = br.readLine();
            System.out.println(nextLine);
        }
    }
}

暂无
暂无

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

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