The Artima Developer Community
Sponsored Link

Java Answers Forum
Object Streams

1 reply on 1 page. Most recent reply: Dec 30, 2002 1:46 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
JamesMoore

Posts: 2
Nickname: jamesmoore
Registered: Dec, 2002

Object Streams Posted: Dec 30, 2002 4:38 AM
Reply to this message Reply
Advertisement
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
public abstract class Space extends Object
public Space()
public abstract String getType()
public abstract double getSize()


Room
public class Room  extends Space
public Room(String type,
           double area)
public String getType()
public double getSize()


Apartment
public class Apartment extends Space
public static final String KITCHEN
public String getType()
public double getSize()
public Room[] getRooms()


---------------------------------------------------
java-file:
public class 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)
*/
 
       public void write(Apartment apartment)
          throws IOException {
          
          outStream = new FileOutputStream(filePath);
          obStream = new ObjectOutputStream(outStream);
               
          obStream.writeObject(apartment);
 
       }
}

----------------------------------------------
import java.io.*;
import java.util.*;
              
 
public class ApartmentWriterProgram 
extends Object
              
   private KeyboardReader keyboardReader; 
   private List roomsInList; 
   private Room[] rooms;
   private int 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. */
 
         public void 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.");
          }
           
       }
              
        public static void main(String[]  args) {
             ApartmentWriterProgram program = new ApartmentWriterProgram();
             program.run(); 
       } 
                     
 
} 


there are the description
http://www.hut.fi/~t106213/english/exercises/unit_6/exercise_5/description_65.html


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Object Streams Posted: Dec 30, 2002 1:46 PM
Reply to this message Reply
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.

Flat View: This topic has 1 reply on 1 page
Topic: net connection problems in java Previous Topic   Next Topic Topic: Can somebody help me please ??

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use