Results 1 to 6 of 6

Thread: [RESOLVED] Read object from file?

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [RESOLVED] Read object from file?

    Hi,

    I think I need to use serializable in-order to read objects in this case "engine" from a file. Although, I don't have any idea how to read the onject. This is what I have done thus far:

    java Code:
    1. public void readFromTextFile(BufferedReader fi) throws IOException {
    2.         this.engine =Integer.parseInt(fi.readLine());
    3.     }
    4.  
    5.     public void writeToTextFile(PrintWriter fo) throws IOException {
    6.           fo.println(this.engine);
    7.     }

    I have been trying to follow this example but I'm unsure whether I need everything in the example?

    Thanks,


    Nightwalker83
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  2. #2
    Junior Member
    Join Date
    Jan 2010
    Posts
    20

    Re: Read object from file?

    Quote Originally Posted by Nightwalker83 View Post
    Hi,

    I think I need to use serializable in-order to read objects in this case "engine" from a file. ...
    Please give some more information about what you're trying to do. "in order to read objects in this case "engine"" -- what case? what engine?

    *Using* serializable is not that difficult -- classes that implement it have methods for writing out an object instance to a file and reading it back in later, with understandable limitations about threads and other such things that don't make sense to serialize.

    *Implementing" serializable can be difficult, but again it depends on what you want to do. If the only parts of your class that need to be saved are themselves serializable, then you don't have to write a special method for them.

    If all you're trying to do is write out value(s) to read in later, then that is outside what Java calls serialization, and not as conceptually difficult.

  3. #3

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Read object from file?

    In this case there is an engine object with some cylinders! The code below writes then reads number of cylinders for a file.

    java Code:
    1. package abstract_and_interface_example;
    2. import java.io.*;
    3.  
    4. public class Engine implements Saveable{
    5.  
    6.   public static final int DEF_NUMCYLINDERS = 4;
    7.   public static final int DEF_CAPACITY = 1300;
    8.  
    9.   private int numCylinders;
    10.   private int capacity;
    11.  
    12.   public Engine(int numCylinders, int capacity) {
    13.     this.numCylinders = numCylinders;
    14.     this.capacity = capacity;
    15.   }
    16.  
    17.   public Engine() {
    18.     this(DEF_NUMCYLINDERS, DEF_CAPACITY);
    19.   }
    20.  
    21.   public int getNumCylinders() {
    22.     return this.numCylinders;
    23.   }
    24.  
    25.   public int getCapacity() {
    26.     return this.capacity;
    27.   }
    28.  
    29.   public void setNumCylinders(int numCylinders) {
    30.     this.numCylinders = numCylinders;
    31.   }
    32.  
    33.   public void setCapacity(int capacity) {
    34.     this.capacity = capacity;
    35.   }
    36.  
    37.  
    38. // The following two methods ar needed in order for this class to implement
    39. // the Saveable interface.
    40.  
    41.   public void writeToBinaryFile(DataOutputStream theFile) throws IOException{
    42.     theFile.writeInt(numCylinders);
    43.     theFile.writeInt(capacity);
    44.   }
    45.  
    46.   public void readFromBinaryFile(DataInputStream theFile) throws IOException {
    47.     numCylinders = theFile.readInt();
    48.     capacity = theFile.readInt();
    49.   }
    50.  
    51.   public Object clone() throws CloneNotSupportedException {
    52.     return super.clone();
    53.   }
    54.  
    55.   public String toString() {
    56.     StringBuilder toStringBuilder = new StringBuilder();
    57.     toStringBuilder.append("[");
    58.     toStringBuilder.append(super.toString());
    59.     toStringBuilder.append(",numCylinders: ");
    60.     toStringBuilder.append(numCylinders);
    61.     toStringBuilder.append(",capacity: ");
    62.     toStringBuilder.append(capacity);
    63.     toStringBuilder.append("]");
    64.     return toStringBuilder.toString();
    65.   }
    66.  
    67.     public void readFromTextFile(BufferedReader fi) throws IOException {
    68.         this.numCylinders =Integer.parseInt(fi.readLine());
    69.         this.capacity = Integer.parseInt(fi.readLine());
    70.     }
    71.  
    72.     public void writeToTextFile(PrintWriter fo) throws IOException {
    73.           fo.println(this.numCylinders);
    74.         fo.println(this.capacity);
    75.     }
    76.  
    77.  }

    What I'm trying to do with the code in the first post is read/write the data in the same way except this time using saving/reading the engine object.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    20

    Re: Read object from file?

    Well, if I understand what you want, you can do it with the following:

    1. declare that your Engine class implements serializable. This does not have any methods in it, so it does not require specific methods be implemented.

    2. code like the following (a method in Engine) will create a file from an Engine instance:

    Code:
      
    public void writeMeOut(String filename) throws IOException
      {
        ObjectOutputStream oos;
                oos = new ObjectOutputStream(new FileOutputStream(filename));
                oos.writeObject(this);
                oos.close();
              }
            }
            catch (IOException ioe) 
              { errorMessage("IO Exception creating serialization file: " 
                  + ioe.getMessage()); 
              }
          }
        }
      }
    And similarly for an ObjectInputStream

    Code:
          ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename)); 
          engine = (Engine)ois.readObject();
          ois.close();
    Is that what you were after?

  5. #5

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Read object from file?

    I think I have misunderstood what needed to do.

    I have done:

    java Code:
    1. public void readFromTextFile(BufferedReader fi) throws IOException {
    2.       super.readFromTextFile(fi);
    3.     }
    4.  
    5.     public void writeToTextFile(PrintWriter fo) throws IOException {
    6.      super.writeToTextFile(fo);
    7.      engine = new Engine();
    8.      engine.writeToTextFile(fo);
    9.     }

    Which, is in a different file called "MotorisedVehicle.Java". Doing the above solves the problem.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6
    Junior Member
    Join Date
    Jan 2010
    Posts
    20

    Re: Read object from file?

    Well, it sounds like you've solved it -- but I don't really know what your question was any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width