gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:51:59 27 4
gpt4 key购买 nike

我真的很想有一种方法可以根据 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com