正如标题所说,我正在寻找一个简短的函数,如果有特定的字符串,它会在文件中读取,如果存在,则执行某些操作。例如,如果在测试中.txt文件存在字符串 TestString。有人会这么好心地给我建议一个方法吗?
InputStream is = //--open an input stream from file--
BufferedReader rd = new BufferedReader(new InputStreamReader(is,"UTF-8"));
String line;
while ( (line = rd.readLine()) != null ){
if(line.matches(".*TestString.*")){ //--regex of what to search--
//--do something---
break; //--if not want to search further--
}
}