-
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());
}
}
}
}
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks