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());
}
}
}
}