1 回答

TA贡献1825条经验 获得超4个赞
它不应该太难:
BufferedReader reader = new BufferedReader(new FileReader(path));
String line;
boolean isWordFound = false;
while ((line = reader.readLine()) != null) {
// add the line in the list if the word was found
if (isWordFound()){
sensor_Daten.add(line);
}
// flag isWordFound to true when the match is done the first time
if (!isWordFound && line.matches(myRegex)){
isWordFound = true;
}
}
作为旁注,您不会在应该关闭流时关闭它。该try-with-resource声明会为您做到这一点。所以你应该赞成这种方式。
概括地说:
BufferedReader reader = ...;
try{
reader = new BufferedReader(new FileReader(path));
}
finally{
try{
reader.close();
}
catch(IOException e) {...}
}
应该只是:
try(BufferedReader reader = new BufferedReader(new FileReader(path))){
...
}
添加回答
举报