See following example :
/**
*
* @author prasobh
*/
import java.io.*;
import java.util.*;
public class ExerciseSerializable {
public static void main(String... aArguments) {
// create a Serializable Map
Map<Integer, String> names = new HashMap<Integer, String>();
names.put(1, "prasobh");
names.put(2, "mohanlal");
names.put(3, "mammokka");
try {
// use buffering
OutputStream file = new FileOutputStream("names.store");
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput output = new ObjectOutputStream(buffer);
try {
output.writeObject(names);
} finally {
output.close();
}
} catch (IOException ex) {
System.out.println("Exception " + ex);
}
// deserialize the quarks.ser file
try {
//use buffering
InputStream file = new FileInputStream("names.store");
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream(buffer);
try {
//deserialize the List
Map<Integer, String> recoverednames = (Map<Integer, String>) input.readObject();
//display its data
System.out.println("recoverednames: " + recoverednames);
} finally {
input.close();
}
} catch (ClassNotFoundException ex) {
System.out.println("Exception " + ex);
} catch (IOException ex) {
System.out.println("Exception " + ex);
}
}
}
/**
*
* @author prasobh
*/
import java.io.*;
import java.util.*;
public class ExerciseSerializable {
public static void main(String... aArguments) {
// create a Serializable Map
Map<Integer, String> names = new HashMap<Integer, String>();
names.put(1, "prasobh");
names.put(2, "mohanlal");
names.put(3, "mammokka");
try {
// use buffering
OutputStream file = new FileOutputStream("names.store");
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput output = new ObjectOutputStream(buffer);
try {
output.writeObject(names);
} finally {
output.close();
}
} catch (IOException ex) {
System.out.println("Exception " + ex);
}
// deserialize the quarks.ser file
try {
//use buffering
InputStream file = new FileInputStream("names.store");
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream(buffer);
try {
//deserialize the List
Map<Integer, String> recoverednames = (Map<Integer, String>) input.readObject();
//display its data
System.out.println("recoverednames: " + recoverednames);
} finally {
input.close();
}
} catch (ClassNotFoundException ex) {
System.out.println("Exception " + ex);
} catch (IOException ex) {
System.out.println("Exception " + ex);
}
}
}
No comments:
Post a Comment