简体   繁体   中英

How to edit a txt file using PrintWriter?

I have the following java file that stores student data (a student number and their surname) in a txt file:

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

    }
}

Upon opening, the code reads in any text in the .txt file and constructs Student objects as required, so that the state of the system persists everytime the code runs.

However, I'm struggling to find a way to edit the .txt file using PrintWriter when I call my editSurname() function. How would I go about isolating the student with a specific student number and then edit the required field?

Use a csv file instead of a txt file. Use OpenCSV to process the records.

OR

  1. Have your txt file 1 record per line.
  2. Separate each field by separator.
  3. Create another temporary text file in memory.
  4. Modify the records and save the records in the temporary file.
  5. Delete the original file.
  6. Rename the temporary file with original file name.

For editing your student record, you need to first read all the records in memory. Create an array of student objects to hold all the records. Perform a binary search on the objects and modify them.

为什么不使用数据库概念,可以使用数据库轻松完成

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM