public class UserOperator {
private static File file = new File("user.txt"); // 这段代码是登陆系统的用户操作类 疑问是 : 此处定义为static 用意何在呢 ?
public boolean login(String username, String password) {
boolean flag = false;
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;
}
public void regist(User user) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file,true));
String data = user.getUsername() + "=" + user.getPassword();
bw.write(data);
bw.newLine();
bw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
|
|