Address.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.io.Serializable; public class Address implements Serializable { String street; String country; public void setStreet(String street) { this.street = street; } public void setCountry(String country) { this.country = country; } public String getStreet() { return this.street; } public String getCountry() { return this.country; } @Override public String toString() { return new StringBuffer(" Street : ") .append(this.street) .append(" Country : ") .append(this.country).toString(); } } |
Serializer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Serializer { public static void main(String args[]) { Serializer serializer = new Serializer(); serializer.serializeAddress("wall street", "united state"); } public void serializeAddress(String street, String country) { Address address = new Address(); address.setStreet(street); address.setCountry(country); try { FileOutputStream fout = new FileOutputStream("c:\\address.ser"); ObjectOutputStream oos = new ObjectOutputStream(fout); oos.writeObject(address); oos.close(); System.out.println("Done"); } catch (Exception ex) { ex.printStackTrace(); } } } |