Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

I am really interested to have a method that can remove a line from text file based on an id number, but I don't know how to achieve this.

The students.txt file looks like:

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

Here is what I have got so far for the code: public 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;
            }
        }
    }
}

2 Answers

You can read from the original file, write to a temporary file only those records that do not match the specified ID and then at the end replace the original file with the 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's 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();
  }
}
like image 71
Jaime Garcia Avatar answered Dec 31 '25 12:12

Jaime Garcia


You can use String's split method. Split on "," and then check index 0 for the ID you're wanting to remove. (I.e. don't print anything in that case.) I'd give example code, but I don't have a Java compiler installed anymore.

like image 25
Benjamin Oakes Avatar answered Dec 31 '25 11:12

Benjamin Oakes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!