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))){
...
}
添加回答
舉報