0

我一直在网上和这里搜索如何删除包含一两个单词的行,但我在 java 上找不到任何东西。这是我现在拥有的代码:

try {
  BufferedReader reader = new BufferedReader(new FileReader("Readfile.txt"));
  String line = reader.readLine();
  while(line !=null)
  {
    for(int i = 0 ; i<newarray.length;i++){
      if(line.contains(newarray[i])){
        System.out.println(line);
      }
    }
    line=reader.readLine();
  }
} catch (Exception ex) {
  System.out.println(ex.getMessage());
}

它从文本文件中读取句子,但在打印出来之前,我想删除一些包含关键字的句子,例如 fun。

4

2 回答 2

2

像这样的东西:

//BufferedReader stuff etc.
List<String> words = new ArrayList<String>();
words.add("fun");
words.add("something");

String line;
while( (line = br.readLine()) != null)
{
   boolean found = false;
   for(String word: words)
   {
       if(line.contains(word))
       {
           found = true;
           break;
       }
   }

   if(found) continue;
   System.out.println(line);
}
于 2013-02-21T15:47:42.867 回答
0
if(line.contains(newarray[i])){
    line = line.replace("fun" ,"");
    System.out.println(line);
  }

试试这个,它会在打印之前删除单词。

于 2013-02-21T15:58:23.160 回答