id number
을 기반으로 한 텍스트 파일에서 한 줄을 지울 수있는 방법이 정말 흥미 롭습니다. 그러나이 방법을 모르겠습니다."studentId"를 기반으로 텍스트 파일에서 행을 제거하는 방법은 무엇입니까?
1111111,John Smith<br/>
7777777,Dave Smith
내가 코드를 지금까지 가지고 무엇을 : 같은
students.txt
파일을 찾습니다
// class Student import java.io.*; public class Student implements Serializable
// class EnrollmentController
{
// 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; } }
import java.util.*; import java.io.*; 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(); /** * Constructor for objects of class EnrollmentController */ 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; } } } }
하고, 질문의 수준, 당신이 적어도 노력을하지 않았다 가정 문제를 해결할 수 있습니다. 코드에 대한 귀하의 구체적인 의심과 무엇을 이미 시도했는지 게시하십시오. – OscarRyz