[英]How to remove a line from text file based on “studentId”?
我真的很有興趣有一個方法可以根據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;
}
}
公共類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();
}
}
您可以使用String
的split
方法。 拆分","
然后檢查索引0
以查找您要刪除的ID。 (即在這種情況下不打印任何內容。)我將提供示例代碼,但我不再安裝Java編譯器。
大概你想讀所有的學生,但不是根據ID寫一個特定的學生。 因此,在這種情況下,請修改writeToStudentFile()方法,使其類似於以下內容:
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);}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.