java - 如何逐行读取文件并存储特定值
问题描述
我需要一些帮助,因为我是 Java 新手,所以我已经坚持了一段时间。我在控制台中创建了一个登录面板,其中设置了默认用户名和密码来登录。他们可以创建一个帐户,将用户名存储在 username.txt 文件中,将密码存储在 passwords.txt 文件中。我想知道如何获取它,以便函数逐行读取用户名和密码的 txt 文件,直到它将用户名与用户名文件中的现有用户名匹配并且与密码相同,以便登录。
这是从文件读取功能:
public static void readFromFile() throws Exception {
BufferedReader saveUsrFile, savePwFile;
try {
saveUsrFile = new BufferedReader(new FileReader("username.txt"));
String line_u = saveUsrFile.readLine();
while (line_u != null && saveUsrFile.readLine().equals(LP.username)) {
cusUsr = saveUsrFile.readLine();
}
saveUsrFile.close();
savePwFile = new BufferedReader(new FileReader("passwords.txt"));
String line_p = savePwFile.readLine();
while (line_p != null && savePwFile.readLine().equals(LP.password)) {
cusPass = savePwFile.readLine();
}
savePwFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
这是登录面板:
import java.util.Scanner;
public class LoginPanel {
static String username;
static String password;
public LoginPanel() {}
public static void main(String[] args) {
boolean flag = true;
Passenger p = new Passenger();
Scanner input = new Scanner(System.in);
while(flag == true) {
System.out.println("Enter Username: ");
username = input.nextLine();
System.out.println("Enter Password: ");
password = input.nextLine();
if ((password.equals(p.DefPass) && username.equals(p.DefUser)) || (password.equals(p.cusPass) && username.equals(p.cusUsr))) {
System.out.println("Passenger login successful!");
p.display();
} else {
System.out.println("Please check your username or password and try again!");
}
}
}
}
有人请帮助我并告诉我如何让它逐行读取文件,直到它匹配并允许用户登录。
解决方案
一个简化的流程就像
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String username, password;
System.out.print("Enter Username: ");
username = input.nextLine();
System.out.print("Enter Password: ");
password = input.nextLine();
if (authenticated(username, password)) {
System.out.println("Passenger login successful!");
} else {
System.out.println("Please check your username or password and try again!");
}
}
static boolean authenticated(String username, String password) {
BufferedReader saveUsrFile, savePwFile;
boolean usrFound = false, pwFound = false;
try {
saveUsrFile = new BufferedReader(new FileReader("username.txt"));
String line_u = saveUsrFile.readLine();
while (line_u != null) {
if (line_u.equals(username)) {
usrFound = true;
break;
}
line_u = saveUsrFile.readLine();
}
saveUsrFile.close();
savePwFile = new BufferedReader(new FileReader("password.txt"));
String line_p = savePwFile.readLine();
while (line_p != null) {
if (line_p.equals(password)) {
pwFound = true;
break;
}
line_p = savePwFile.readLine();
}
savePwFile.close();
} catch (IOException e) {
e.printStackTrace();
}
return usrFound && pwFound;
}
}
示例运行:
Enter Username: hello
Enter Password: bye
Please check your username or password and try again!
另一个示例运行:
Enter Username: hello
Enter Password: Abc123
Passenger login successful!
username.txt 的内容:
hello
world
abc
xyz
hi
bye
password.txt 的内容:
Abc123
456Abc
Xyz123
456Xyz
123Abc
123Xyz
注意:在这个演示中,username
并password
没有检查它们在各自文件中的顺序。
推荐阅读
- reactjs - 如何通过映射material-ui网格组件添加json
- java - 线程内一致性
- reactjs - 如何从带有子元素的数组中附加反应 div 元素?以及如何为映射的父元素提供唯一的“关键”道具
- drake - 我对德雷克模拟感到困惑
- android - 使用 react-native-wifi-reborn 在 android 和 ios 上使用 react-native 自动连接 wifi
- javascript - jQuery Datatables 错误(未捕获的 TypeError:$ 不是函数)
- c++ - 尝试使用 cuda 进行图像模糊
- ubuntu - 使用 ubuntu18.04 在 WSL2 上运行 sudo snap
- android - 如何在我的用户中访问 Firebase 变量 AcX?
- javascript - useEffect 中的 now.getTime() 不起作用