因此,我正在尝试为学校项目创建一个简单的银行应用程序。我的Authenticate类使用在GUI中输入的用户名和密码,并使用BufferedReader读取与该用户名关联的文件。
public Authenticate(String user, String pw){
username = user;
password = pw;
}
public void Login() throws FileNotFoundException, IOException{
try (BufferedReader read = new BufferedReader(new FileReader(fileLocation+username))) {
String i;
//Checking password
i=read.readLine();
pass = password.equals(i);
//Checking if user is admin
i=read.readLine();
isAdmin = "manager".equals(i);
}
}
文件位置为“ C:\ Users \ rayle \ Desktop \ FinalProject \ BankProject \”,用户名为cat。然后,文件中的第一行是密码,第二行是客户或经理,第三行是他们在帐户中有多少钱。这部分很不错。
public Balance(String user){
username = user;
}
public String getBalance() throws FileNotFoundException, IOException{
try (BufferedReader read = new BufferedReader(new FileReader(fileLocation+username))) {
String last=null, line;
while((line=read.readLine()) != null){
last = line;
} currentBal = last;
}
return currentBal;
}
因此,这第二个类仅想读取帐户中的金额,但是每当尝试尝试时,我都会收到以下java.io.FileNotFoundException:C:\ Users \ rayle \ Desktop \ FinalProject \ BankProject(访问被拒绝)。如果我在BufferedReader中使用(fileLocation +“ cat”),则程序运行正常。
答案 0 :(得分:-2)
完成所需操作后,您需要关闭资源。然后可以重新打开它。我的建议是,当您打开资源时,使用数据结构来进行所需的数据处理。