Java Reference
In-Depth Information
16. }
17. serialize(fileName);
18. }
19.
20. public static void serialize(String fileName){
21. try {
22. serializeToFile(createData(), fileName);
23. }
24. catch (IOException exc){
25. exc.printStackTrace();
26. }
27. }
28.
29. private static Serializable createData(){
30. Contact contact1 = new ContactImpl("Dennis", "Moore", "Managing Director", "Highway
Man, LTD");
31. Contact contact2 = new ContactImpl("Joseph", "Mongolfier", "High Flyer","Lighter
than Air Productions");
32. Contact contact3 = new ContactImpl("Erik", "Njoll", "Nomad without Portfolio",
"Nordic Trek, Inc.");
33. Contact contact4 = new ContactImpl("Lemming", "", "Principal Investigator", "BDA");
34.
35. Project project = new Project("IslandParadise", "Acquire a personal island paradise",
contact2);
36.
37. Task task1 = new Task(project, "Fortune", "Acquire a small fortune", contact4, true);
38. Task task2 = new Task(project, "Isle", "Locate an island for sale", null, true);
39. Task task3 = new Task(project, "Name", "Decide on a name for the island", contact3,
false);
40. project.addProjectItem(task1);
41. project.addProjectItem(task2);
42. project.addProjectItem(task3);
43.
44. Task task4 = new Task(task1, "Fortune1", "Use psychic hotline to predict winning
lottery numbers", null, false);
45. Task task5 = new Task(task1, "Fortune2", "Invest winnings to ensure 50% annual
interest", contact1, true);
46. Task task6 = new Task(task2, "Isle1", "Research whether climate is better in the
Atlantic or Pacific", contact1, true);
47. Task task7 = new Task(task2, "Isle2", "Locate an island for auction on EBay", null,
false);
48. Task task8 = new Task(task2, "Isle2a", "Negotiate for sale of the island", null,
false);
49. Task task9 = new Task(task3, "Name1", "Research every possible name in the world",
null, true);
50. Task task10 = new Task(task3, "Name2", "Eliminate any choices that are not
coffee-related", contact4, false);
51. task1.addProjectItem(task4);
52. task1.addProjectItem(task5);
53. task2.addProjectItem(task6);
54. task2.addProjectItem(task7);
55. task2.addProjectItem(task8);
56. task3.addProjectItem(task9);
57. task3.addProjectItem(task10);
58. return project;
59. }
60.
61. private static void serializeToFile(Serializable content, String fileName) throws
IOException {
62. ObjectOutputStream serOut = new ObjectOutputStream(new FileOutputStream(fileName));
63. serOut.writeObject(content);
64. serOut.close();
65. }
66. }
Example A.38 DataRetriever.java
1. import java.io.File;
2. import java.io.FileInputStream;
3. import java.io.IOException;
4. import java.io.ObjectInputStream;
5.
6. public class DataRetriever{
7. public static Object deserializeData(String fileName){
8. Object returnValue = null;
9. try{
10. File inputFile = new File(fileName);
11. if (inputFile.exists() && inputFile.isFile()){
Search WWH ::




Custom Search