blob: c847a7f0890b15171c88dc8e125e53c39b724b8a (
plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
/* Listing4111.java */
import java.io.*;
import java.util.*;
public class Listing4111
{
public static Object seriaClone(Object o)
throws IOException, ClassNotFoundException
{
//Serialisieren des Objekts
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(o);
os.flush();
//Deserialisieren des Objekts
ByteArrayInputStream in = new ByteArrayInputStream(
out.toByteArray()
);
ObjectInputStream is = new ObjectInputStream(in);
Object ret = is.readObject();
is.close();
os.close();
return ret;
}
public static void main(String[] args)
{
try {
//Erzeugen des Buchobjekts
Book book = new Book();
book.author = "Peitgen, Heinz-Otto";
String[] s = {"J�rgens, Hartmut", "Saupe, Dietmar"};
book.coAuthors = s;
book.title = "Bausteine des Chaos";
book.publisher = "rororo science";
book.pubyear = 1998;
book.pages = 514;
book.isbn = "3-499-60250-4";
book.reflist = new Vector();
book.reflist.addElement("The World of MC Escher");
book.reflist.addElement(
"Die fraktale Geometrie der Natur"
);
book.reflist.addElement("G�del, Escher, Bach");
System.out.println(book.toString());
//Erzeugen und Ver�ndern der Kopie
Book copy = (Book)seriaClone(book);
copy.title += " - Fraktale";
copy.reflist.addElement("Fractal Creations");
//Ausgeben von Original und Kopie
System.out.print(book.toString());
System.out.println("---");
System.out.print(copy.toString());
} catch (IOException e) {
System.err.println(e.toString());
} catch (ClassNotFoundException e) {
System.err.println(e.toString());
}
}
}
class Book
implements Serializable
{
public String author;
public String[] coAuthors;
public String title;
public String publisher;
public int pubyear;
public int pages;
public String isbn;
public Vector reflist;
public String toString()
{
String NL = System.getProperty("line.separator");
StringBuffer ret = new StringBuffer(200);
ret.append(author + NL);
for (int i = 0; i < coAuthors.length; ++i) {
ret.append(coAuthors[i] + NL);
}
ret.append("\"" + title + "\"" + NL);
ret.append(publisher + " " + pubyear + NL);
ret.append(pages + " pages" + NL);
ret.append(isbn + NL);
Enumeration e = reflist.elements();
while (e.hasMoreElements()) {
ret.append(" " + (String)e.nextElement() + NL);
}
return ret.toString();
}
}
|