0

私はオンラインで検索していて、1つまたは2つの単語を含む行を削除する方法についてここで検索していますが、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 に答える