使用对象输入/输出流完成用户注册登录界面,User类(用户名,密码) 保存在工程文件夹res\password.txt中
时间: 2024-03-17 14:42:20 浏览: 37
好的,您需要在工程中创建一个名为 "res" 的文件夹,并将 "password.txt" 文件放入该文件夹中。然后,您可以使用以下代码实现用户注册和登录功能:
```java
import java.io.*;
public class UserAuthentication {
private static final String FILE_PATH = "res/password.txt";
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Welcome to User Authentication System.");
// Register
System.out.println("\n-----Registration-----");
System.out.print("Enter your username: ");
String username = null;
try {
username = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print("Enter your password: ");
String password = null;
try {
password = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (registerUser(username, password)) {
System.out.println("Registration successful.");
} else {
System.out.println("Registration failed. User already exists.");
}
// Login
System.out.println("\n-----Login-----");
System.out.print("Enter your username: ");
username = null;
try {
username = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print("Enter your password: ");
password = null;
try {
password = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (loginUser(username, password)) {
System.out.println("Login successful.");
} else {
System.out.println("Login failed. Invalid username or password.");
}
}
private static boolean registerUser(String username, String password) {
try {
// Check if user already exists
if (userExists(username)) {
return false;
}
// Add new user
BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH, true));
writer.write(username + ":" + password);
writer.newLine();
writer.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private static boolean loginUser(String username, String password) {
try {
BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH));
String line = null;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(":");
if (parts[0].equals(username) && parts[1].equals(password)) {
reader.close();
return true;
}
}
reader.close();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private static boolean userExists(String username) {
try {
BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH));
String line = null;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(":");
if (parts[0].equals(username)) {
reader.close();
return true;
}
}
reader.close();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
```
您可以运行该代码,按照提示输入用户名和密码进行注册和登录。该程序会将用户信息保存在 "res/password.txt" 文件中,并在登录时读取该文件进行验证。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)