You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.9 KiB
75 lines
1.9 KiB
package com.ybw.mathapp.util;
|
|
|
|
import com.ybw.mathapp.entity.User;
|
|
import java.io.*;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class LoginFileUtils {
|
|
private static final String USER_FILE = "users.txt";
|
|
|
|
// 读取所有用户
|
|
// FileUtils.java 中的 readUsers 方法(简化版)
|
|
public static List<User> readUsers() {
|
|
List<User> users = new ArrayList<>();
|
|
File file = new File(USER_FILE);
|
|
|
|
if (!file.exists()) {
|
|
try {
|
|
file.createNewFile();
|
|
} catch (IOException e) {
|
|
System.err.println("创建用户文件失败: " + e.getMessage());
|
|
}
|
|
return users;
|
|
}
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
|
String line;
|
|
while ((line = reader.readLine()) != null) {
|
|
line = line.trim();
|
|
if (line.isEmpty()) continue;
|
|
|
|
User user = User.fromString(line);
|
|
if (user != null) {
|
|
users.add(user);
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
System.err.println("读取用户文件失败: " + e.getMessage());
|
|
}
|
|
return users;
|
|
}
|
|
|
|
// 保存用户到文件
|
|
public static void saveUser(User user) {
|
|
try (PrintWriter writer = new PrintWriter(new FileWriter(USER_FILE, true))) {
|
|
writer.println(user.toString());
|
|
} catch (IOException e) {
|
|
System.err.println("保存用户信息失败: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
// 检查邮箱是否已注册
|
|
public static boolean isEmailRegistered(String email) {
|
|
List<User> users = readUsers();
|
|
for (User user : users) {
|
|
if (user.getEmail().equalsIgnoreCase(email)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// 验证用户登录
|
|
public static boolean validateUser(String email, String password) {
|
|
List<User> users = readUsers();
|
|
for (User user : users) {
|
|
if (user.getEmail().equalsIgnoreCase(email) &&
|
|
user.getPassword().equals(password)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|