parent
e135d2b66e
commit
8682876ebb
@ -1,76 +1,77 @@
|
||||
package util;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Utility class to modify specific fields in user.txt.
|
||||
* 替换了 ModifyClass.java 和 ModifyPassword.java。
|
||||
*/
|
||||
public class ModifyUtils {
|
||||
|
||||
private static List<String> readAllLines(String filePath, List<String> lines) throws IOException {
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
lines.add(line);
|
||||
}
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
private static void writeAllLines(String filePath, List<String> lines) throws IOException {
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
|
||||
for (String l : lines) {
|
||||
writer.write(l + System.lineSeparator());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int findTargetLine(List<String> lines, String targetName) {
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
String line = lines.get(i).trim();
|
||||
if (line.isEmpty()) continue;
|
||||
|
||||
String[] parts = line.split("\\s+");
|
||||
if (parts.length > 0 && parts[0].equals(targetName)) {
|
||||
return i; // 0-based index
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 核心逻辑:修改文件中特定用户的某个字段 (0-based)
|
||||
public static int modifyFileField(String filePath, String targetName,
|
||||
int fieldIndex, String newValue) {
|
||||
List<String> lines = new ArrayList<>();
|
||||
try {
|
||||
readAllLines(filePath, lines);
|
||||
} catch (IOException e) {
|
||||
System.err.println("读取文件错误: " + e.getMessage());
|
||||
return -1;
|
||||
}
|
||||
|
||||
int idx = findTargetLine(lines, targetName);
|
||||
if (idx == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
String[] parts = lines.get(idx).trim().split("\\s+");
|
||||
if (parts.length > fieldIndex) {
|
||||
parts[fieldIndex] = newValue;
|
||||
lines.set(idx, String.join(" ", parts));
|
||||
try {
|
||||
writeAllLines(filePath, lines);
|
||||
return idx + 1; // 返回 1-based 行号
|
||||
} catch (IOException e) {
|
||||
System.err.println("写入文件错误: " + e.getMessage());
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
System.out.println("目标行元素不足。");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
package util;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Utility class to modify specific fields in user.txt.
|
||||
* 替换了 ModifyClass.java 和 ModifyPassword.java。
|
||||
*/
|
||||
public class ModifyUtils {
|
||||
|
||||
private static List<String> readAllLines(String filePath, List<String> lines) throws IOException {
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
lines.add(line);
|
||||
}
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
private static void writeAllLines(String filePath, List<String> lines) throws IOException {
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
|
||||
for (String l : lines) {
|
||||
writer.write(l + System.lineSeparator());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int findTargetLine(List<String> lines, String targetName) {
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
String line = lines.get(i).trim();
|
||||
if (line.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
String[] parts = line.split("\\s+");
|
||||
if (parts.length > 0 && parts[0].equals(targetName)) {
|
||||
return i; // 0-based index
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 核心逻辑:修改文件中特定用户的某个字段 (0-based)
|
||||
public static int modifyFileField(String filePath, String targetName,
|
||||
int fieldIndex, String newValue) {
|
||||
List<String> lines = new ArrayList<>();
|
||||
try {
|
||||
readAllLines(filePath, lines);
|
||||
} catch (IOException e) {
|
||||
System.err.println("读取文件错误: " + e.getMessage());
|
||||
return -1;
|
||||
}
|
||||
|
||||
int idx = findTargetLine(lines, targetName);
|
||||
if (idx == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
String[] parts = lines.get(idx).trim().split("\\s+");
|
||||
if (parts.length > fieldIndex) {
|
||||
parts[fieldIndex] = newValue;
|
||||
lines.set(idx, String.join(" ", parts));
|
||||
try {
|
||||
writeAllLines(filePath, lines);
|
||||
return idx + 1; // 返回 1-based 行号
|
||||
} catch (IOException e) {
|
||||
System.err.println("写入文件错误: " + e.getMessage());
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
System.out.println("目标行元素不足。");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue