1 回答

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個(gè)贊
它不應(yīng)該太難:
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;
}
}
作為旁注,您不會(huì)在應(yīng)該關(guān)閉流時(shí)關(guān)閉它。該try-with-resource聲明會(huì)為您做到這一點(diǎn)。所以你應(yīng)該贊成這種方式。
概括地說(shuō):
BufferedReader reader = ...;
try{
reader = new BufferedReader(new FileReader(path));
}
finally{
try{
reader.close();
}
catch(IOException e) {...}
}
應(yīng)該只是:
try(BufferedReader reader = new BufferedReader(new FileReader(path))){
...
}
添加回答
舉報(bào)