Created
September 10, 2018 07:57
-
-
Save nasrabadiAM/aeef81257afb98a20fbb0fe2c2442a43 to your computer and use it in GitHub Desktop.
Way to Serialize a Serializable Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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