Skip to content

Instantly share code, notes, and snippets.

@nasrabadiAM
Created September 10, 2018 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasrabadiAM/aeef81257afb98a20fbb0fe2c2442a43 to your computer and use it in GitHub Desktop.
Save nasrabadiAM/aeef81257afb98a20fbb0fe2c2442a43 to your computer and use it in GitHub Desktop.
Way to Serialize a Serializable Object
Person person = new Person();
person.setAge(22);
person.setName("Ali");
//write serializable object
FileOutputStream fileOutputStream = new FileOutputStream("file.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(person);
objectOutputStream.flush();
objectOutputStream.close();
//read serializable object
FileInputStream fileInputStream = new FileInputStream("file.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Person person2 = (Person) objectInputStream.readObject();
objectInputStream.close();
//person2 equals person
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment