How to delete a line from a text file based on "studentId"?

I am very interested in having a method that can remove a line from a text based file id number, but I don't know how to achieve this.

The file students.txtlooks like this:

1111111,John Smith<br/> 
7777777,Dave Smith

Here is what I got so far for the code: Open class. Student:

public class Student  {
    // instance variables
    private int studentId;
    private String name;

    /**
     * Constructor for objects of class Student
     */
    public Student(int id, String name) {
        this.name = name;
        studentId = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String newName) {
        name = newName;
    }

    public void setId(int newId) {
        studentId = newId;
    }

    public int getId() {
        return studentId;
    }
}

public class EnrollmentController:

public class EnrollmentController {
    private Student theStudent;
    private BufferedWriter writer;
    private BufferedReader reader;
    private final File studentFile = new File("students.txt");
    private ArrayList students = new ArrayList();

    public EnrollmentController() {
        readFromStudentFile();

    }

    public ArrayList getStudents() {
        return students;
    }

    public void addStudent(int id, String name) {
        students.add(new Student(id, name));
    }

    public void printClassList(String courseId) {

    }

    public void writeToStudentFile(int id, String name) {
        try {
            writer = new BufferedWriter(new FileWriter(studentFile, true));
            writer.write(id + "," + name + "\n");
            writer.flush();
            writer.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    public void readFromStudentFile() {
        students = new ArrayList();

        try {
            reader = new BufferedReader(new FileReader(studentFile));

            String line = reader.readLine();

            while (line != null) {
                String[] record = line.split(",");
                int id = Integer.parseInt(record[0]);
                Student s = new Student(id, record[1]);
                students.add(s);
                line = reader.readLine();
            }

            reader.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    public Student findStudent(int id) {
        boolean found = false;
        Iterator it = students.iterator();
        while (it.hasNext() && !found) {
            Student s = (Student) it.next();
            if (s.getId() == id) {
                found = true;
                return s;
            }
        }
        return null;
    }

    {
        boolean found = false;
        {
            {
                found = true;
            }
        }
    }
}
+3
source share
3 answers

You can read from the source file, write to the temporary file only those records that do not match the specified ID, and then at the end replace the original file with a new temporary file.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;
public class Demo{
  public static void main(String[] argv)
                        throws Exception{
    Writer output = new BufferedWriter(new FileWriter("fixed.txt"));
    BufferedReader freader =
     new BufferedReader(new FileReader("orinal.txt"));
    String s;
    while ((s=freader.readLine())!=null){
      String tokens[] = s.split(",");
      String id = f[0];
      String name = f[1];
      // check against the ID or ID you don't want. If the current ID is not the one
      // you don't want then write it to the output file
      if (!id.equals("00001") {
          output.write(s + "\n");
      }
    }
    freader.close();
    output.close();
  }
}
+6
source

String split. ",", 0 , . ( .) , Java.

+2

Presumably, you want to read all students, but not write a specific student based on the identifier. Therefore, in this case, modify your writeToStudentFile () method to look like this:

public void writeToStudentFile(int idToIgnore)
{
    try{
        writer = new BufferedWriter(new FileWriter(studentFile, true));
        Iterator it = students.iterator();
        while (it.hasNext())
        {
            Student s = (Student)it.next();
            if (s.getId() != idToIgnore)
            {
                writer.write(s.getId() + "," + s.getName() + "\n");
            }
        }
        writer.flush();
        writer.close();
    }
    catch(IOException e){System.out.println(e);}
}
+1
source

Source: https://habr.com/ru/post/1714306/


All Articles