|
| 1 | +import java.io.FileInputStream; |
| 2 | +import java.io.FileOutputStream; |
| 3 | +import java.io.ObjectInputStream; |
| 4 | +import java.io.ObjectOutputStream; |
| 5 | +import java.util.ArrayList; |
| 6 | + |
| 7 | +public class JavaSerializationExample { |
| 8 | + public static void main(String args[]){ |
| 9 | + StudentClass studentClass=new StudentClass(); |
| 10 | + studentClass.setId(1); |
| 11 | + studentClass.setName("Rahul"); |
| 12 | + studentClass.setAddress("Delhi"); |
| 13 | + studentClass.setCourse("BCA"); |
| 14 | + |
| 15 | + System.out.println("Serialization Example First Writing Data into File"); |
| 16 | + |
| 17 | + try { |
| 18 | + FileOutputStream fileOutputStream = new FileOutputStream("student.txt"); |
| 19 | + ObjectOutputStream objectOutputStream=new ObjectOutputStream(fileOutputStream); |
| 20 | + objectOutputStream.writeObject(studentClass); |
| 21 | + objectOutputStream.flush(); |
| 22 | + objectOutputStream.close(); |
| 23 | + System.out.println("Writed in File"); |
| 24 | + } |
| 25 | + catch (Exception e){ |
| 26 | + System.out.println("Error in Opening File"); |
| 27 | + } |
| 28 | + |
| 29 | + System.out.println("Reading Serialized Data from File : "); |
| 30 | + |
| 31 | + try { |
| 32 | + FileInputStream fileInputStream=new FileInputStream("student.txt"); |
| 33 | + ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); |
| 34 | + StudentClass studentClass1=(StudentClass) objectInputStream.readObject(); |
| 35 | + System.out.println("Student Data :"); |
| 36 | + System.out.println("Name : "+studentClass1.getName()); |
| 37 | + } |
| 38 | + catch (Exception e){ |
| 39 | + System.out.println("Error in Reading File "); |
| 40 | + } |
| 41 | + |
| 42 | + System.out.println("Arraylist Serialization Example : "); |
| 43 | + ArrayList<StudentClass> studentList=new ArrayList<>(); |
| 44 | + |
| 45 | + StudentClass studentClass1=new StudentClass(); |
| 46 | + studentClass1.setName("Aman"); |
| 47 | + studentClass1.setCourse("BCA"); |
| 48 | + studentClass1.setAddress("Delhi"); |
| 49 | + studentClass1.setId(2); |
| 50 | + |
| 51 | + StudentClass studentClass2=new StudentClass(); |
| 52 | + studentClass2.setName("Vishal"); |
| 53 | + studentClass2.setCourse("MCA"); |
| 54 | + studentClass2.setAddress("Delhi"); |
| 55 | + studentClass2.setId(3); |
| 56 | + |
| 57 | + StudentClass studentClass3=new StudentClass(); |
| 58 | + studentClass3.setName("Rahul"); |
| 59 | + studentClass3.setCourse("B.SC"); |
| 60 | + studentClass3.setAddress("Delhi"); |
| 61 | + studentClass3.setId(4); |
| 62 | + |
| 63 | + studentList.add(studentClass1); |
| 64 | + studentList.add(studentClass2); |
| 65 | + studentList.add(studentClass3); |
| 66 | + |
| 67 | + System.out.println("Writing List Data "); |
| 68 | + try { |
| 69 | + FileOutputStream fileOutputStream = new FileOutputStream("student_list.txt"); |
| 70 | + ObjectOutputStream objectOutputStream=new ObjectOutputStream(fileOutputStream); |
| 71 | + objectOutputStream.writeObject(studentList); |
| 72 | + objectOutputStream.flush(); |
| 73 | + objectOutputStream.close(); |
| 74 | + System.out.println("Write Successfully List in File"); |
| 75 | + } |
| 76 | + catch (Exception e){ |
| 77 | + System.out.println("Error in Opening File"); |
| 78 | + } |
| 79 | + |
| 80 | + try { |
| 81 | + FileInputStream fileInputStream = new FileInputStream("student_list.txt"); |
| 82 | + ObjectInputStream objectInputStream=new ObjectInputStream(fileInputStream); |
| 83 | + |
| 84 | + ArrayList<StudentClass> studentClasses= (ArrayList<StudentClass>) objectInputStream.readObject(); |
| 85 | + for(StudentClass studentClass4:studentClasses){ |
| 86 | + System.out.println("Name : "+studentClass4.getName()); |
| 87 | + } |
| 88 | + } |
| 89 | + catch (Exception e){ |
| 90 | + System.out.println("Error in Reading File "); |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | +} |
0 commit comments