+2  A: 

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();
  }
}
ferrari fan
Hi there thank you for the solution, but can you please demonstrate this through the code, cause i am total beginner in Java. thank you
There you go, it's rough but it should work. Look at articles online about reading/writing comma-delimited text files using java.
ferrari fan
A: 

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.

Benjamin Oakes
A: 

Presumably you want to read all the students, but not write a specific student based on the ID. So in that case, modify your writeToStudentFile() method so it's similar to the following:

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