首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在一个文件中搜索字符串,然后在另一个文件中搜索该字符串

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

Stack Overflow用户
提问于 2019-04-09 21:53:21
回答 2查看 97关注 0票数 2

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

代码语言:javascript
复制
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包含Home,File2.txt包含Home,因此应该打印Home

EN

回答 2

Stack Overflow用户

发布于 2019-04-09 22:15:23

首先,您需要读取第一个文件。最好是SET(),因为它会去掉重复的字符串。您将拥有set1

完成后,您需要读取第二个文件,并执行相同的操作。你会得到set2

现在,您需要在参数为set2set1上使用RemoveAll()方法。set1中剩余的内容需要打印在屏幕上。你可以用lambda做到这一点。

请参阅THIS了解如何读取文件。

请看下面的代码:

代码语言:javascript
复制
    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
EN

Stack Overflow用户

发布于 2019-04-09 23:21:10

使用Regex来满足特定需求,下面是针对您的问题的重构代码。如果这对你有效,请告诉我。

代码语言:javascript
复制
 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内容

代码语言:javascript
复制
Homeee
Ho
hello
hom
xx
Me

file2.txt内容

代码语言:javascript
复制
Home
Me
H
Homey

结果

代码语言:javascript
复制
Word not found Homeee
Word not found Ho
Word not found hello
Word not found hom
Word not found xx
Word found Me
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55594544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档