Java ObjectInputStream and Vector help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Java ObjectInputStream and Vector help

  1. #1
    Join Date
    Oct 2005
    Posts
    4

    Arrow Java ObjectInputStream and Vector help

    The issue with this IoBookmarks class is it can only read the first object back from the Bookmarks.dat file and then throws java.io.StreamCorruptedException Any suggestions would be much appreciated!.

    Code:

    public IoBookmarks(Vector vectorBookmarks) {

    myVector = vectorBookmarks;
    }

    public void ReadObject(){

    try{

    bookmarksFile = new File("Bookmarks.dat");
    inStream = new FileInputStream (bookmarksFile);
    objInStream = new ObjectInputStream (inStream);

    myVector.setSize (1);

    try{
    while ((myObjectBookmark = (ObjBookmark) objInStream.readObject()) != null){

    myVector.addElement (myObjectBookmark);
    System.out.println("read object...");
    System.out.println(myObjectBookmark.getName());
    System.out.p rintln(myObjectBookmark.getAddress());
    System.out.println(myObjectBookmark.ge tDate());
    System.out.println(myObjectBookmark.getTime());
    }
    }
    catch( ClassNotFoundException cnfEx){

    System.out.println("class not found exception..." + cnfEx.getMessage ());
    }
    }
    catch(FileNotFoundException fnfe){

    System.out.println("file not found exception..." + fnfe.getMessage());
    }
    catch(IOException io){

    System.out.println("io exception..." + io.getMessage());
    }
    finally{

    try{
    if(objInStream != null){
    objInStream.close();
    }
    }
    catch(IOException io){
    System.out.println("io exception..."+io.getMessage());
    }
    }

    public void WriteObject(){

    try{
    bookmarksFile = new File("Bookmarks.dat");
    outStream = new FileOutputStream (bookmarksFile,true);
    objOutStream = new ObjectOutputStream (outStream);

    myVector.trimToSize();

    for(int b = 0;b < myVector.size();b++){

    myObjectBookmark = (ObjBookmark)(myVector.get(b));

    System.out.println("write object...");
    System.out.println(myObjectBookmark.getName());
    System.out.p rintln(myObjectBookmark.getAddress());
    System.out.println(myObjectBookmark.ge tDate());
    System.out.println(myObjectBookmark.getTime());

    objOutStream .writeObject (myObjectBookmark);
    }

    }

    catch(FileNotFoundException fnfe){

    System.out.println("file not found exception..." + fnfe.getMessage());
    }
    catch(IOException io){

    System.out.println("io exception..." + io.getMessage());
    }
    finally{

    try{
    if(objOutStream != null){
    objOutStream.flush ();
    objOutStream.close ();
    }
    }
    catch(IOException io){
    System.out.println("io exception.."+io.getMessage());
    }
    }
    }

    }

  2. #2
    Join Date
    Oct 2005
    Posts
    4

    Arrow Solution - It Works

    Two things I changed in the code,first I was appending to the FileOutputStream but this was corrupting the object Bookmarks.dat file, so now there is no appending just read at program start,write when exiting.The second thing when reading the file into the vector and it was throwing a io exception,well it still does but now I return the vector from the exceptions and program continues to execute.

    I don't think this is correct programming standard but for now it works, if any bright ideas please let us know - Theo

    public class IoBookmarks {

    private FileOutputStream outStream;
    private ObjectOutputStream objOutStream;
    private ObjBookmark myObjectBookmark;
    private Vector myVector;
    private File bookmarksFile;
    private FileInputStream inStream;
    private ObjectInputStream objInStream;


    public IoBookmarks () {

    }

    public void WriteObject (Vector myVector){

    try{
    bookmarksFile = new File ("Bookmarks.dat");
    outStream = new FileOutputStream (bookmarksFile);
    objOutStream = new ObjectOutputStream (outStream);

    myVector.trimToSize ();

    for(int b = 0;b < myVector.size ();b++){

    myObjectBookmark = (ObjBookmark)(myVector.get (b));

    System.out.println ("\n\nwriting object..." + b);
    System.out.println (myObjectBookmark.getName ());
    System.out.println (myObjectBookmark.getAddress ());
    System.out.println (myObjectBookmark.getDate ());
    System.out.println (myObjectBookmark.getTime ());

    objOutStream.writeObject (myObjectBookmark);
    }
    }

    catch(FileNotFoundException fnfe){

    System.out.println ("file not found exception..." + fnfe.getMessage ());
    } catch(IOException io){

    System.out.println ("main try io exception..." + io.getMessage ());
    } finally{

    try{
    if(objOutStream != null){
    objOutStream.flush ();
    objOutStream.close ();
    }
    } catch(IOException io){
    System.out.println ("finally io exception.."+io.getMessage ());
    }
    }
    }


    public Vector ReadObject (){

    Vector myVector = new Vector(1);

    try{

    bookmarksFile = new File ("Bookmarks.dat");
    inStream = new FileInputStream (bookmarksFile);
    objInStream = new ObjectInputStream (inStream);


    while ((myObjectBookmark = (ObjBookmark) objInStream.readObject()) != (null)){

    myVector.addElement (myObjectBookmark);
    }
    }
    catch(ClassNotFoundException cnfEx){

    System.out.println ("class not found exception..." + cnfEx.getMessage ());
    return myVector;

    } catch(FileNotFoundException fnfe){

    System.out.println ("file not found exception..." + fnfe.getMessage ());
    return myVector;
    }

    catch(IOException io){

    System.out.println ("main try io exception..." + io.getMessage ());
    return myVector;
    }


    finally{

    try{
    if(objInStream != null){
    objInStream.close ();
    }

    } catch(IOException io){
    System.out.println ("finally io exception..."+io.getMessage ());
    return myVector;
    }
    }


    return myVector;
    }

    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center