从 txt 文件加载一个 int 和一个加密的字符串
Loading an int and an encrypted String from txt file
我正在研究 class 项目的密码登录部分。没有什么花哨。用户或角色将是一个 int,密码是一个字符串。我现在只是使用简单的加密。我遇到的问题是在读取文件时输入不匹配。我过去做过类似的事情,要求我阅读整数和字符串,但没有任何问题。但我无法弄清楚在这种情况下出了什么问题。对于我为什么会收到此错误的任何帮助,将不胜感激。我正在使用 while(inputStream.hasNextLine())
,然后阅读 int
,然后阅读 String
我已经尝试了 hasNextInt
和 hasNext
并不断收到相同的错误。
public void readFile(){
Scanner inputStream = null;
try {
inputStream = new Scanner (new FileInputStream("login.txt"));
}catch (FileNotFoundException e) {
e.printStackTrace();
}
if(inputStream != null){
while (inputStream.hasNextLine()){
int luser = inputStream.nextInt();
String lpass = inputStream.nextLine();
newFile[count] = new accessNode(luser, lpass);
count ++;
}
inputStream.close();
}
}
尝试将其作为字符串读取并将字符串转换为 int
while (inputStream.hasNextLine()) {
Integer luser = Integer.parseInt(inputStream.nextLine());
String lpass = inputStream.nextLine();
newFile[count] = new accessNode(luser, lpass);
count++;
}
但您需要确保您的文件中的数据格式完全如下
12342
password
很难说不知道你遇到了什么错误,但我猜这是因为你没有阅读整个文件。
您的文件可能如下所示:
1\r\n
password\r\n
当您调用 nextInt() 时,它会读取 int,但不会超过第一个 \r\n 因此,当您调用 nextLine() 时,它会读取到行尾,所以您得到的只是 \r\n。你需要先读过去\r\n然后再读密码
尝试
int luser = inputStream.nextInt();
inputStream.nextLine();
String lpass = inputStream.nextLine();
newFile[count] = new accessNode(luser, lpass);
我正在研究 class 项目的密码登录部分。没有什么花哨。用户或角色将是一个 int,密码是一个字符串。我现在只是使用简单的加密。我遇到的问题是在读取文件时输入不匹配。我过去做过类似的事情,要求我阅读整数和字符串,但没有任何问题。但我无法弄清楚在这种情况下出了什么问题。对于我为什么会收到此错误的任何帮助,将不胜感激。我正在使用 while(inputStream.hasNextLine())
,然后阅读 int
,然后阅读 String
我已经尝试了 hasNextInt
和 hasNext
并不断收到相同的错误。
public void readFile(){
Scanner inputStream = null;
try {
inputStream = new Scanner (new FileInputStream("login.txt"));
}catch (FileNotFoundException e) {
e.printStackTrace();
}
if(inputStream != null){
while (inputStream.hasNextLine()){
int luser = inputStream.nextInt();
String lpass = inputStream.nextLine();
newFile[count] = new accessNode(luser, lpass);
count ++;
}
inputStream.close();
}
}
尝试将其作为字符串读取并将字符串转换为 int
while (inputStream.hasNextLine()) {
Integer luser = Integer.parseInt(inputStream.nextLine());
String lpass = inputStream.nextLine();
newFile[count] = new accessNode(luser, lpass);
count++;
}
但您需要确保您的文件中的数据格式完全如下
12342
password
很难说不知道你遇到了什么错误,但我猜这是因为你没有阅读整个文件。
您的文件可能如下所示:
1\r\n
password\r\n
当您调用 nextInt() 时,它会读取 int,但不会超过第一个 \r\n 因此,当您调用 nextLine() 时,它会读取到行尾,所以您得到的只是 \r\n。你需要先读过去\r\n然后再读密码
尝试
int luser = inputStream.nextInt();
inputStream.nextLine();
String lpass = inputStream.nextLine();
newFile[count] = new accessNode(luser, lpass);