如何在文件中搜索字符串,然后在另一个文件中搜索该字符串

时间:2019-04-09 13:53:21

标签: java

我正在尝试创建一个Java程序,该程序可以读取名为file1.txt的文件并存储其字符串,并将这些字符串搜索到另一个名为file2.txt的文件中,如果找不到匹配项,则从file1打印该特定字符串.txt。

public static void main(String[] args)
{
    try
    {
        BufferedReader word_list = new BufferedReader(new FileReader("file1.txt"));
        BufferedReader eng_dict = new BufferedReader(new FileReader("file2.txt"));

        String spelling_word = word_list.readLine();
        String eng_dict_word = eng_dict.readLine();

        while (spelling_word != null)
        {
            System.out.println(spelling_word);
            spelling_word = word_list.readLine();

            if(eng_dict_word.contains(spelling_word))
            {
                System.out.println("Word found "+spelling_word);
            }
            else
            {
                System.out.println("Word not found "+spelling_word);
            }
        }
        word_list.close();
    }
    catch (IOException e) 
    {
        e.printStackTrace();
    }
}

现在,我可以从file1.txt中获取数据,但是无法搜索file1的数据,例如,搜索file2.txt中的单词“ Home”

看到这里File1.txt包含Homee,而File2.txt具有Home,因此应该打印Homee

enter image description here

2 个答案:

答案 0 :(得分:1)

首先,您需要阅读第一个文件。最好使用SET(),因为它将消除重复的字符串。您将拥有set1

完成此操作后,您需要读取第二个文件,然后执行相同的操作。您将获得set2

现在,您需要使用RemoveAll()作为参数,在set1上使用set2方法。 set1中剩余的内容需要在scren上打印。您可以使用lambda来实现。

请参见THIS,以了解如何读取文件。

请参见下面的代码:

    Set<String> set1 = new HashSet<>();
    Set<String> set2 = new HashSet<>();

    try (FileReader reader = new FileReader("file1.txt");
         BufferedReader br = new BufferedReader(reader)) {

        // read line by line
        String line;
        while ((line = br.readLine()) != null) {
            set1.add(line);
        }

    } catch (IOException e) {
        System.err.format("IOException: %s%n", e);
    }

    try (FileReader reader = new FileReader("file2.txt");
         BufferedReader br = new BufferedReader(reader)) {

        // read line by line
        String line;
        while ((line = br.readLine()) != null) {
            set2.add(line);
        }

    } catch (IOException e) {
        System.err.format("IOException: %s%n", e);
    }

    set1.removeAll(set2);
    set1.forEach(System.out::println);

答案 1 :(得分:0)

使用Regex满足特定需求,下面是针对您的问题的重构代码。让我知道这是否适合您。

 public static void main(String[] args) throws IOException
 {

    try
    {
        BufferedReader word_list = new BufferedReader(new FileReader("resources/file1.txt"));
        BufferedReader eng_dict = new BufferedReader(new FileReader("resources/file2.txt"));

        String spelling_word = word_list.readLine();
        String eng_dict_word = eng_dict.readLine();

        int matchFound = 0;
        Matcher m = null;

        while (spelling_word != null)
        {
            // creating the pattern for checking for the exact match on file2.txt
            String spelling_word_pattern = "\\b" + spelling_word + "\\b";

            Pattern p = Pattern.compile(spelling_word_pattern);

            while(eng_dict_word !=null) {
                m = p.matcher(eng_dict_word);
                if(m.find()) {
                    matchFound = 1;
                    break;
                }
                eng_dict_word = eng_dict.readLine();
            }

            if(matchFound == 1) {
                System.out.println("Word found " + m.group());
            }else {
                System.out.println("Word not found "+ spelling_word);
            }

            spelling_word = word_list.readLine();
            eng_dict = new BufferedReader(new FileReader("resources/file2.txt"));
            eng_dict_word = eng_dict.readLine();
            matchFound = 0;
        }


        word_list.close();
        eng_dict.close();
    }
    catch (IOException e) 
    {
        e.printStackTrace();
    }
}

file1.txt内容

Homeee
Ho
hello
hom
xx
Me

file2.txt内容

Home
Me
H
Homey

结果

Word not found Homeee
Word not found Ho
Word not found hello
Word not found hom
Word not found xx
Word found Me