Getting a class cast exception while using ObjectInputStream.

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 637 Views - Last Post: 06 June 2014 - 08:27 PM Rate Topic: -----

#16 jacobTheMechanic  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 89
  • Joined: 18-September 12

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 08:27 PM

Here's my improved methods:

Method to serialize the Collection and not each object:
        public void saveOperations() throws IOException{
                FileOutputStream fos = new FileOutputStream(fileName);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                    //for(int i=0; i<opList.size(); i++){
                        oos.writeObject(opList);
                    //}
                    //oos.writeObject(new EndOfFile());
                oos.close();
        }



And the one to read the data:

      public void checkLoadPreviousStationStatus() throws FileNotFoundException, IOException,
                                                     ClassNotFoundException, EOFException, TempArrayOutOfBoundsException{        
            Object loadOpn = null;
            File f = new File(fileName);
            //if station file already exists load the info
                if(f.exists() && !f.isDirectory()){
                    FileInputStream fis = new FileInputStream(f);
                    ObjectInputStream ois = new ObjectInputStream(fis);
                        //while(!((loadOpn = ois.readObject()) instanceof EndOfFile)){
                                opList = (LinkedList<Operation>)ois.readObject();
                        //}
                    ois.close();
                    sortStationOperations();
                }else{
                    getOperationsFromDatabase();
                }
        }



Again, thanks a lot g00se and smohd for the help!

This post has been edited by jacobTheMechanic: 06 June 2014 - 08:29 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2