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