Java Reference
In-Depth Information
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. }else{
18. System.err.println(inputFile + " is a directory.");
19. }
20. }else{
21. System.err.println("File " + inputFile + " does not exist.");
22. }
23. }catch (ClassNotFoundException exc){
24. exc.printStackTrace();
25. }catch (IOException exc){
26. exc.printStackTrace();
27. }
28. return returnValue;
29. }
30. public static void storeData(File outputFile, Serializable data){
31. try{
32. ObjectOutputStream writeOut = new ObjectOutputStream(new
FileOutputStream(outputFile));
33. writeOut.writeObject(data);
34. writeOut.close();
35. }catch (IOException exc){
36. exc.printStackTrace();
37. }
38. }
39. }
The interface Address and its implementer AddressImpl provide storage for address objects in this example.
Example A.195 Address.java
1. import java.io.Serializable;
2. public interface Address extends Serializable{
3. public static final String EOL_STRING = System.getProperty("line.separator");
4. public static final String SPACE = " ";
5. public static final String COMMA = ",";
6. public String getAddress();
7. public String getType();
8. public String getDescription();
9. public String getStreet();
10. public String getCity();
11. public String getState();
12. public String getZipCode();
13.
14. public void setType(String newType);
15. public void setDescription(String newDescription);
16. public void setStreet(String newStreet);
17. public void setCity(String newCity);
18. public void setState(String newState);
19. public void setZipCode(String newZip);
20. }
Example A.196 AddressImpl.java
1. public class AddressImpl implements Address{
2. private String type;
3. private String description;
4. private String street;
5. private String city;
6. private String state;
7. private String zipCode;
8. public static final String HOME = "home";
9. public static final String WORK = "work";
10.
11. public AddressImpl(){ }
12. public AddressImpl(String newDescription, String newStreet,
Search WWH ::




Custom Search