我一直在网上和这里搜索如何删除包含一两个单词的行,但我在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());
}
它从文本文件中读取句子,但在打印它们之前,我想删除一些包含关键字的句子,例如乐趣。
最佳答案
类似这样的事情:
//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);
}
关于java - java中删除包含关键字的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15006236/