java - 如何从基于 "studentId"的文本文件中删除一行?

标签 java

我真的很想有一种方法可以根据 id number 从文本文件中删除一行,但我不知道如何实现。

students.txt 文件如下所示:

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

这是我目前所获得的代码: 公开课学生:

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)类 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;
            }
        }
    }
}

最佳答案

您可以从原始文件中读取,只将那些与指定 ID 不匹配的记录写入临时文件,然后在最后用新的临时文件替换原始文件。

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();
  }
}

关于java - 如何从基于 "studentId"的文本文件中删除一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1227560/

相关文章:

java - 延迟打印 Java 形状

java - 编译器能够优化引用创建吗?

java - 当字符串以 &*( 开头并以 )(* 结尾时,如何从字符串中删除一部分文本

java - Hibernate 将数据库列重置为默认值?

java - 按名称、居民和大小对 ArrayList of countries 进行排序

java - Scala 符号到 java 枚举

java - 使用Tomcat 5.0配置Struts2.1.6时出现异常

java - 使用私钥生成 JSON Web token (JWT)

java - JSF 页面的最大大小

java - 如何使用正则表达式匹配某类词前的所有内容