fork(1) download
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.io.Serializable;
  9. import java.util.ArrayList;
  10.  
  11.  
  12.  
  13. public class Student implements Serializable
  14. {
  15.  
  16. private String name;
  17.  
  18. public void setName(String name)
  19. {
  20. this.name=name;
  21. }
  22. public String getName()
  23. {
  24. return name;
  25. }
  26. public String toString()
  27. {
  28.  
  29. return name;
  30. }
  31.  
  32.  
  33. public static void main(String [] args) throws IOException, ClassNotFoundException
  34. {
  35.  
  36. ArrayList studentList=new ArrayList();
  37. Student obj1=new Student();
  38. Student obj2=new Student();
  39. obj1.setName("Aman");
  40. obj2.setName("Arora");
  41. FileOutputStream fs=new FileOutputStream(new File("aman"));
  42.  
  43. studentList.add(obj1);
  44. studentList.add(obj2);
  45. o.writeInt(p.size());// size of arraylist.
  46. o.writeObject(obj1);
  47. o.writeObject(obj2);
  48. studentList.removeAll(studentList);// removed all the students from ArrayList, so that we can fill it with objects read from file
  49. int size=ois.readInt();
  50. System.out.println(size);
  51. for(int i=0;i<size;i++)
  52. {
  53. Student obj=(Student)ois.readObject();
  54. System.out.println(obj.getName());
  55. studentList.add(obj);// read Student objects from file and added to ArrayList.
  56. }
  57. System.out.println(studentList);
  58.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:60: error: reached end of file while parsing
		System.out.println(studentList);
		                                ^
1 error
stdout
Standard output is empty