1. Serialization Example
SerializableClass.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 | package javautility; import java.io.Serializable; public class SerializableClass implements Serializable{ private static final long serialVersionUID = 1L; String name; int age; public SerializableClass() { } public SerializableClass(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
ObjectStream.java (Optional)
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package javautility; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectStream { public InputStream StringToStream( String s ) { return new ByteArrayInputStream(s.getBytes()); } public InputStream ByteArrayToSteam( byte[] b ) { return new ByteArrayInputStream(b); } public void saveObjectToFile( String filename, Object obj ) throws IOException, FileNotFoundException { FileOutputStream fos = new FileOutputStream(filename); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(obj); fos.close(); oos.close(); } public Object loadObjectFromFile(String filename) throws FileNotFoundException, IOException, ClassNotFoundException { FileInputStream fis = new FileInputStream(filename); ObjectInputStream ois = new ObjectInputStream(fis); Object obj = ois.readObject(); fis.close(); ois.close(); return obj; } public Object loadObjectFromByte(byte[] data) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data)); Object uca = ois.readObject(); ois.close(); return uca; } public byte[] loadByteFromFile(String filename) throws FileNotFoundException, IOException, ClassNotFoundException { File f = new File(filename); byte[] data = new byte[(int)f.length()]; FileInputStream fis = new FileInputStream(filename); fis.read(data); return data; } } |
Serialize.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package javautility; import java.io.FileNotFoundException; import java.io.IOException; public class Serialize { public static void main(String[] args) throws IOException, FileNotFoundException, ClassNotFoundException { String filename = "object.dat"; SerializableClass uca1[] = { new SerializableClass( "Alice", 30 ), new SerializableClass( "Tom", 33 ), new SerializableClass( "Willy", 27 ) }; ObjectStream os = new ObjectStream(); os.saveObjectToFile(filename,uca1); SerializableClass uca2[] = (SerializableClass[]) os.loadObjectFromFile(filename); for( int i=0; i < uca2.length; i++ ) { System.out.println( "name=" + uca2[i].getName() + ", age=" + uca2[i].getAge()); } } } |
Result:
name=Alice, age=30 name=Tom, age=33 name=Willy, age=27
댓글 없음:
댓글 쓰기