Java Reference
In-Depth Information
22. public void setLastName(String newLastName){ lastName = newLastName; }
23. public void setTitle(String newTitle){ title = newTitle; }
24. public void setOrganization(String newOrganization){ organization = newOrganization; }
25.
26. public String toString(){
27. return firstName + SPACE + lastName;
28. }
29. }
Example A.187 Location.java
1. import java.io.Serializable;
2. public interface Location extends Serializable{
3. public String getLocation();
4. public void setLocation(String newLocation);
5. }
Example A.188 LocationImpl.java
1. public class LocationImpl implements Location{
2. private String location;
3.
4. public LocationImpl(){ }
5. public LocationImpl(String newLocation){
6. location = newLocation;
7. }
8.
9. public String getLocation(){ return location; }
10.
11. public void setLocation(String newLocation){ location = newLocation; }
12.
13. public String toString(){ return location; }
14. }
FileLoader class provides methods to load the Appointment collection from a file and save it to a file when
required.
Example A.189 FileLoader.java
1. import java.io.File;
2. import java.io.FileInputStream;
3. import java.io.FileOutputStream;
4. import java.io.IOException;
5. import java.io.ObjectInputStream;
6. import java.io.ObjectOutputStream;
7. import java.io.Serializable;
8. public class FileLoader{
9. public static Object loadData(File inputFile){
10. Object returnValue = null;
11. try{
12. if (inputFile.exists()){
13. if (inputFile.isFile()){
14. ObjectInputStream readIn = new ObjectInputStream(new
FileInputStream(inputFile));
15. returnValue = readIn.readObject();
16. readIn.close();
17. }
18. else {
19. System.err.println(inputFile + " is a directory.");
20. }
21. }
22. else {
23. System.err.println("File " + inputFile + " does not exist.");
24. }
25. }
26. catch (ClassNotFoundException exc){
27. exc.printStackTrace();
28.
29. }
30. catch (IOException exc){
31. exc.printStackTrace();
32.
33. }
34. return returnValue;
35. }
36. public static void storeData(File outputFile, Serializable data){
37. try{
Search WWH ::




Custom Search