-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserManager.java
More file actions
61 lines (56 loc) · 2.01 KB
/
Copy pathUserManager.java
File metadata and controls
61 lines (56 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package auth;
import java.io.*;
public class UserManager {
private static final String FILE_NAME = "data/users.txt";
private static final File FILE = new File(FILE_NAME);
public static boolean isUserExists(String username) {
try {
if (!FILE.exists()) return false;
BufferedReader reader = new BufferedReader(new FileReader(FILE));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts[0].equalsIgnoreCase(username)) {
reader.close();
return true;
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static boolean validateUser(String username, String password) {
try {
if (!FILE.exists()) return false;
BufferedReader reader = new BufferedReader(new FileReader(FILE));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 2 && parts[0].equalsIgnoreCase(username) && parts[1].equals(password)) {
reader.close();
return true;
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static boolean registerUser(String username, String password) {
if (isUserExists(username)) return false;
try {
FILE.getParentFile().mkdirs();
BufferedWriter writer = new BufferedWriter(new FileWriter(FILE, true));
writer.write(username + "," + password);
writer.newLine();
writer.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}