简体   繁体   English

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

[英]How to search string in a file and then search that string in another file

I'm trying to create a java program that can read a file named file1.txt and store its strings and search those strings to another file named file2.txt and if the match is not found then print that particular string from file1.txt.我正在尝试创建一个 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();
    }
}

Right now I'm able to get data from file1.txt but unable to search file1's data for example to search word "Home" in file2.txt现在我可以从 file1.txt 获取数据,但无法搜索 file1 的数据,例如在 file2.txt 中搜索单词“Home”

See that here File1.txt contains Homee and File2.txt has Home, so Homee should be print看到这里 File1.txt 包含 Homee,File2.txt 包含 Home,所以 Homee 应该打印

在此处输入图片说明

first, you need to read first file.首先,您需要读取第一个文件。 Preferably to SET() as it will get rid of duplicate strings.最好是SET()因为它会消除重复的字符串。 You will have set1你将有set1

When this is done, you need to read second file, and do the same.完成后,您需要读取第二个文件,并执行相同的操作。 You will get set2你会得到set2

Now, you have need to use RemoveAll() method on set1 with set2 as parameter.现在,您需要在set1上使用RemoveAll()方法,将set2作为参数。 What is remaining in set1 needs to be printed on scren. set1 中剩余的内容需要打印在屏幕上。 You can do it with lambda.你可以用 lambda 来做到这一点。

See THIS to get how to read file.请参阅以获取如何读取文件。

see code below:见下面的代码:

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

Use Regex for the specific need, below is the refactored code for your problem.针对特定需求使用正则表达式,以下是针对您的问题的重构代码。 Let me know if this works for you.让我知道这是否适合您。

 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 content file1.txt 内容

Homeee
Ho
hello
hom
xx
Me

file2.txt content file2.txt 内容

Home
Me
H
Homey

Result结果

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

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

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