二级JAVA第六章辅导:Java串行化
文章作者 100test 发表时间 2007:03:10 18:45:10
来源 100Test.Com百考试题网
1. 串行化例子一
ObjectInputStream和ObjectOutputStream。
public class SerializationDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Vector list.
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in)).
System.out.print("Checking for previous serialized list").
try {
FileInputStream fin = new FileInputStream("list.out").
ObjectInputStream oin = new ObjectInputStream(fin).
try {
Object obj = oin.readObject().
list = (Vector) obj.
} catch (ClassCastException e) {
// TODO: handle exception
list = new Vector().
} catch (ClassNotFoundException e){
list = new Vector().
}
fin.close().
} catch (FileNotFoundException e) {
// TODO: handle exception
list = new Vector().
}
for (..){
System.out.println("Menu :-").
System.out.println("1.. Add item").
System.out.println("2.. Delete item").
System.out.println("3.. List items").
System.out.println("4.. Save and quit").
System.out.println("Choice").
String response = reader.readLine().
int choice = Integer.parseInt(response).
switch (choice) {
case 1:
System.out.print("Enter item : ").
String item = reader.readLine().
list.addElement(item).