• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Cannot append data to a file

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
I have 4 java classes:

But for some reasons only the last record entered is printed.I also tried to append data to the file,like this:

oos = new ObjectOutputStream(new FileOutputStream( "file.ser",true));

but then it starts giving me some error.can someone please tell me whats wrong,and how can i see the total records entered into the file.

And my second question is what is a blocked/fielded data file??

Please answer my question friends.
Thanks

[ October 10, 2005: Message edited by: jas oberai ]
[ October 10, 2005: Message edited by: jas oberai ]
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Opening the same file for writing twice is a Bad Idea, especially with ObjectOutputStream. OOS writes a header as soon as it opens, so it's probably overwriting existing data. You can't append objects to a file of serialized objects (again, the header). If you want to write multiple objects using OOS, I suggest you put the objects into a collection (i.e. an ArrayList) and serialize that.
 
Jas Oberai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi joe,
i have coded everything and ow i wont like to r-write it again using Collections.I have exactly the same problem that you discussed with this person over here:
https://coderanch.com/t/277067/Streams/java/Appending-file-containing-serialized-Objects

Can you please elaborate on what you said..and tell me how can i accomplish this.
Also,the same was discussed here,but very hard to understand:
http://forum.java.sun.com/thread.jspa?threadID=581792&tstart=0

I just want to store student records in a file,and read them..so do i need to have this serialization feature??Or I can get rid of it and make my life easier.
Thanks
[ October 10, 2005: Message edited by: jas oberai ]
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was quoting from the serialization faq in that topic. I've never done it, so I can't give you much more insight.
If you want alternatives, it really depends on what your requirements are. If you just want to write data out, plain text files may work. If you want to be able to load one record at a time in random order, RandomAccessFile may be your answer.
 
Jas Oberai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joe,
I am using Vectors now as you said.I am able to write data but when I try to read it gives me this error:



This is my code to read the data:

If I uncomment the do-while loop,everything goes fine and it displays one record.But I want to display all the records.Can someone please tell me how to solve this error ,I will be very much thankful to you.
Thanks
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like you are corrupting the stream. What does your write code look like? Are you still opening the output stream twice? Still trying to append to the output stream?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our IO forum...
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about using RandomAccessFile ? Get the end of the file using: long pointer = raf.getFilePointer() and point to this location using raf.seek(pointer) as the place to start writing to.


Jose Cardoso
SCJP
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic