检查集合中的单词是否等于外部文件中的单词

check if the word in the set equals word in outside file

我有一套单词和一个外部文件。 我想检查集合中的一个词是否已经存在于外部文件中。如果单词已经在文件中,则什么都不做,如果单词不在外部文件中,则将其添加到外部文件中。 这是我写的代码:

public static void toFile(Set<String> vocab, String filename)
    {   
        try 
        {
            for(String vocabWord : vocab)
            {
                File file = new File(filename);   
                Scanner sc2 = new Scanner(file);      
                
                while(sc2.hasNextLine()) 
                {
                    String docWord = sc2.nextLine();
                    if (!(vocabWord.equals(docWord))) 
                    {
                        FileWriter myWriter = new FileWriter(filename, true);
                        PrintWriter printWriter = new PrintWriter(myWriter);
        
                        printWriter.println(vocabWord);
                        printWriter.close();
                    }
                    else
                        break;
                } 
            }
        }
        catch(IOException e) 
        {
            e.printStackTrace();
        }
    }

我正在使用三个不同的文本文档对其进行测试,分别为“测试文件一”、“测试文件二”和“测试文件三”。 我期待的输出是:“测试文件三”(它与一个停止列表相连,其中一个和两个是其中的一部分,并且一直在工作) 但是,当我 运行 它时,无论只有一个文件还是连续三个文件,文件总是空的。 我尝试更改方法中的内容,但没有任何效果,我要么陷入无限循环,要么在外部文件中什么也没有。 我不确定我错过了什么......我真的很感激任何帮助。

我尝试了这个并添加了一些评论以进行解释。我已经在本地机器上测试过,它可以工作

 public static void toFile(Set<String> vocab, String filename) {
    try {
        for(String vocabWord : vocab) {
            //task for each String in our Set
            File file = new File(filename);
            Scanner sc2 = new Scanner(file);

            boolean exists = false;//lets say it doesn't exist
            while(sc2.hasNextLine()) {
                //task for each line in the text
                //search the whole file first for the word 
                String docWord = sc2.nextLine();
                if (docWord.equals(vocabWord)){
                    exists = true;
                    break;
                }

            }
            if (!exists) {
                //add the vocabWord only if it doesnt exists
                FileWriter myWriter = new FileWriter(filename, true);
                PrintWriter printWriter = new PrintWriter(myWriter);

                printWriter.println(vocabWord);
                printWriter.close();
            }
        }
    } catch(IOException e) {
        e.printStackTrace();
    }
}

将缺失的词汇按词表顺序追加,可以减少文件操作 因此:

public static void toFile(Set<String> vocab, String filename) {
    try {
        Charset charset = Charset.defaultCharset();
        Path path = Paths.get(filename);
        Set<String> existing = Files.lines(path, charset)
            .collect(Collectors.toSet());
        if (!existing.isEmpty()) {
            try (BufferedWriter bw = Files.newBufferedWriter(path, charset,
                     StandardOpenOption.APPEND);
                 PrintWriter printWriter = new PrintWriter(bw)) {
                vocab.stream()
                        .filter(word -> !existing.contains(word))
                        .forEach(word -> printWriter.println(word));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}