我一直在网上和在这里搜索如何删除包含一两个单词的行,但我在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());
}
它从文本文件中读取句子,但在打印出来之前,我想删除一些包含关键字的句子,例如:乐趣。
答案 0 :(得分: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);
}
答案 1 :(得分:0)
if(line.contains(newarray[i])){
line = line.replace("fun" ,"");
System.out.println(line);
}
试试这个,它会在打印前删除该单词。