【问题标题】:check if the word in the set equals word in outside file检查集合中的单词是否等于外部文件中的单词
【发布时间】:2020-11-08 22:55:39
【问题描述】:

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

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

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

【问题讨论】:

    标签: java file io set word


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      要按词汇顺序追加缺少的词汇,可以减少文件操作 像这样:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-18
        • 2018-04-21
        • 2019-12-29
        相关资源
        最近更新 更多