我正在尝试编写一个简单的登录应用程序,记录登录.txt文件的所有尝试。目前我的应用程序只会记录上次尝试的内容。有什么指针吗?
以下是我的一些代码:
public boolean authenticate(String user, String pword) {
boolean isValid = false;
if (user.equalsIgnoreCase("Test")
&& pword.equals("password")) {
isValid = true;
}
return isValid;
}
public static void accessLog(String username) {
String timestamp = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
// declaring variables of log and initializing the buffered writer
String log = "Username: " + username + " Attempted login timestamp: " + timestamp + "\n";
BufferedWriter writer = null;
//write the log variable using the bufferedWriter to log.txt
try {
writer = new BufferedWriter(new FileWriter("Log.txt"));
writer.write(log);
} //print error message if there is one
catch (IOException io) {
System.out.println("File IO Exception" + io.getMessage());
} //close the file
finally {
try {
if (writer != null) {
writer.close();
}
} //print error message if there is one
catch (IOException io) {
System.out.println("Issue closing the file." + io.getMessage());
}
}
}
答案 0 :(得分:0)
添加一个参数有什么问题?
`boolean isSuccess = authenticate(user, password);
accessLog(username, isSuccess);
public static void accessLog(String username, boolean isSuccess) {
//code here
}
`
答案 1 :(得分:0)
我能用这个来解决我的问题:
writer = new BufferedWriter(new FileWriter("Log.txt",true));