Guest User

Untitled

a guest
May 20th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. // class Student
  2. import java.io.*;
  3.  
  4. public class Student implements Serializable
  5. {
  6. // instance variables
  7. private int studentId;
  8. private String name;
  9.  
  10. /**
  11. * Constructor for objects of class Student
  12. */
  13. public Student(int id, String name)
  14. {
  15. this.name = name;
  16. studentId = id;
  17. }
  18.  
  19.  
  20. public String getName()
  21. {
  22. return name;
  23. }
  24.  
  25. public void setName(String newName)
  26. {
  27. name = newName;
  28. }
  29.  
  30. public void setId(int newId)
  31. {
  32. studentId = newId;
  33. }
  34.  
  35. public int getId()
  36. {
  37. return studentId;
  38. }
  39.  
  40. }
  41.  
  42. import java.io.BufferedReader;
  43. import java.io.FileReader;
  44. import java.io.FileWriter;
  45. import java.io.Writer;
  46. public class Demo{
  47. public static void main(String[] argv)
  48. throws Exception{
  49. Writer output = new BufferedWriter(new FileWriter("fixed.txt"));
  50. BufferedReader freader =
  51. new BufferedReader(new FileReader("orinal.txt"));
  52. String s;
  53. while ((s=freader.readLine())!=null){
  54. String tokens[] = s.split(",");
  55. String id = f[0];
  56. String name = f[1];
  57. // check against the ID or ID's you don't want. If the current ID is not the one
  58. // you don't want then write it to the output file
  59. if (!id.equals("00001") {
  60. output.write(s + "n");
  61. }
  62. }
  63. freader.close();
  64. output.close();
  65. }
  66. }
  67.  
  68. public void writeToStudentFile(int idToIgnore)
  69. {
  70. try{
  71. writer = new BufferedWriter(new FileWriter(studentFile, true));
  72. Iterator it = students.iterator();
  73. while (it.hasNext())
  74. {
  75. Student s = (Student)it.next();
  76. if (s.getId() != idToIgnore)
  77. {
  78. writer.write(s.getId() + "," + s.getName() + "n");
  79. }
  80. }
  81. writer.flush();
  82. writer.close();
  83. }
  84. catch(IOException e){System.out.println(e);}
  85. }
Add Comment
Please, Sign In to add comment