本帖最后由 侯丛政 于 2013-2-16 15:24 编辑
public boolean login(String username, String password) {
boolean flag = false;
BufferedReader br = null; // 在处理异常时为什么把 BufferedReader br 单独定义在外面, 并且置为null ?
try {
br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {
String data = line;
String[] strArray = data.split("=");
if (strArray[0].equals(username)
&& strArray[1].equals(password)) {
flag = true;
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
|