Java Reference
In-Depth Information
13. String newCity, String newState, String newZipCode){
14. description = newDescription;
15. street = newStreet;
16. city = newCity;
17. state = newState;
18. zipCode = newZipCode;
19. }
20.
21. public String getType(){ return type; }
22. public String getDescription(){ return description; }
23. public String getStreet(){ return street; }
24. public String getCity(){ return city; }
25. public String getState(){ return state; }
26. public String getZipCode(){ return zipCode; }
27.
28. public void setType(String newType){ type = newType; }
29. public void setDescription(String newDescription){ description = newDescription; }
30. public void setStreet(String newStreet){ street = newStreet; }
31. public void setCity(String newCity){ city = newCity; }
32. public void setState(String newState){ state = newState; }
33. public void setZipCode(String newZip){ zipCode = newZip; }
34.
35. public String toString(){
36. return description;
37. }
38. public String getAddress(){
39. return description + EOL_STRING + street + EOL_STRING +
40. city + COMMA + SPACE + state + SPACE + zipCode + EOL_STRING;
41. }
42. }
The DataCreator class creates a test file with a set of sample addresses.
Example A.197 DataCreator.java
1. import java.io.Serializable;
2. import java.io.ObjectOutputStream;
3. import java.io.FileOutputStream;
4. import java.io.IOException;
5. import java.util.ArrayList;
6. public class DataCreator{
7. private static final String DEFAULT_FILE = "data.ser";
8.
9. public static void main(String [] args){
10. String fileName;
11. if (args.length == 1){
12. fileName = args[0];
13. }else{
14. fileName = DEFAULT_FILE;
15. }
16. serialize(fileName);
17. }
18.
19. public static void serialize(String fileName){
20. try{
21. serializeToFile(createData(), fileName);
22. } catch (IOException exc){
23. exc.printStackTrace();
24. }
25. }
26.
27. private static Serializable createData(){
28. ArrayList items = new ArrayList();
29. items.add(new AddressImpl("Home address", "1418 Appian Way", "Pleasantville", "NH",
"27415"));
30. items.add(new AddressImpl("Resort", "711 Casino Ave.", "Atlantic City", "NJ",
"91720"));
31. items.add(new AddressImpl("Vacation spot", "90 Ka'ahanau Cir.", "Haleiwa", "HI",
"41720"));
32. return items;
33. }
34.
35. private static void serializeToFile(Serializable data, String fileName) throws
IOException {
36. ObjectOutputStream serOut = new ObjectOutputStream(new
FileOutputStream(fileName));
37. serOut.writeObject(data);
38. serOut.close();
Search WWH ::




Custom Search