我有以下 java 文件,它将学生数据(学生编号和他们的姓氏)存储在 txt 文件中:
import java.util.*;
import java.io.*;
public class Students {
    static Student[] studentArray = new Student[100];
    static int currentStudents = 0;
    Scanner console = new Scanner(System.in);
    File log = new File("log.txt");
    PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(log, true)));
    Scanner input = new Scanner(log);
    public static void main(String[] args) {
        String logNo;
        String logSurname;
        if(!(input.hasNextLine())) {
            System.out.println("No student data has been loaded.")
        }
        while(input.hasNextLine()) {
            logNo = input.next();
            logSurname = input.next();
            addStudent(logNo, logSurname);
                    input.nextLine();
        }
        String number;
        String surname;
        System.out.println("Please input details:");
        System.out.printf("\n");
        System.out.println("Student number: ");
        number = console.nextLine();
        System.out.println("Student surname: ");
        surname = console.nextLine();
        output.println(number+"\t"+surname);
        addStudent(number, surname);
        editSurname();
        output.close();
    }
    public static void addStudent(String number, String surname) {
        studentArray[currentStudents] = new Student(number, surname);
    }
    public static void editSurname() {
        String newSurname;
        System.out.println("Please input student number:");
        // find student with inputted student number
        System.out.println("Please enter new surname");
        // set surname to another using Student method
    }
}
打开时,代码会读入 .txt 文件中的任何文本并Student根据需要构造对象,以便每次代码运行时系统状态都保持不变。
PrintWriter但是,当我调用我的editSurname()函数时,我正在努力寻找一种方法来编辑 .txt 文件。我将如何使用特定的学号来隔离学生,然后编辑必填字段?