ApartmentWriterProgram asks information about appartments and saves it to a file with help of ApartmentFileHandler.
There happens IOException in the method write(Apartment apartment) because class Apartment doesn?t implement Serializable as it should (I use ObjectOutputStream).
Apartment has given me in class-file, so I can?t change it to implement Serializable. Is there still some other way to use Object Streams? what about to make a subclass of Appartment, does it help?
---------------------------------------------------- I have javadocs and class-files about:
Space
publicabstractclass Space extends Object
public Space()
publicabstract String getType()
publicabstractdouble getSize()
Room
publicclass Room extends Space
public Room(String type,
double area)
public String getType()
publicdouble getSize()
Apartment
publicclass Apartment extends Space
publicstaticfinal String KITCHEN
public String getType()
publicdouble getSize()
public Room[] getRooms()
publicclass ApartmentFileHandler extends Object implements Serializable {
private OutputStream outStream;
private ObjectOutputStream obStream;
private String filePath;
public ApartmentFileHandler(String filePath) {
this.filePath = filePath;
}
/** Writes the given apartment data in the file. If the file already
exists, the old contents are overwritten.
Parameters:
apartment - the apartment to be written in the file
Throws:
IOException - if other problems arose when handling the file (e.g. the
file could not be written)
*/
publicvoid write(Apartment apartment)
throws IOException {
outStream = new FileOutputStream(filePath);
obStream = new ObjectOutputStream(outStream);
obStream.writeObject(apartment);
}
}
----------------------------------------------
import java.io.*;
import java.util.*;
publicclass ApartmentWriterProgram
extends Object
private KeyboardReader keyboardReader;
private List roomsInList;
private Room[] rooms;
privateint numberOfRooms;
private String file;
public ApartmentWriterProgram()
this.keyboardReader = new KeyboardReader();
this.roomsInList = new ArrayList();
this.rooms = new Room [numberOfRooms];
}
/** Runs the writer program. The program asks the user for some
apartment data and a file name, and saves the data in the file. */
publicvoid run() {
try {
numberOfRooms = keyboardReader.getInt("Give number of rooms: ");
for (int i =1; i<numberOfRooms+1; i++) {
String type =keyboardReader.getString("Give " +i+ ". " + "type of the room: ");
double area =keyboardReader.getDouble("Give " +i+ ". " + "area of room: ");
Room room= new Room(type, area);
this.roomsInList.add(room);
}
roomsInList.toArray(rooms);
Apartment apartment = new Apartment(rooms);
System.out.println();
file = keyboardReader.getString("Give name of the file: ");
ApartmentFileHandler handler = new ApartmentFileHandler(file);
handler.write(apartment);
} catch (IOException ioe) {
System.out.println("Writing to file "+file+ " failed.");
}
}
publicstaticvoid main(String[] args) {
ApartmentWriterProgram program = new ApartmentWriterProgram();
program.run();
}
}
Hmm... I think you are out of luck if the base class didn't implement Serializable. If it has getter methods for all the relevant data, you could just save all the data "manually" and then reconstitute the object from that data.