aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Problem Writing to FileOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Problem Writing to FileOutputStream" Watch "Problem Writing to FileOutputStream" New topic
Author

Problem Writing to FileOutputStream

Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
I tried writing a new record to db.db file once and the write was succesful. If I continue to write to the same record the writes are successful. But when I try writing to another record, somehow I get a bunch of garbage in my
db.db file. There may be an obvious reason for this, but because I haven't done any File I/O before this project, I don't have a clue why this would be happening. Any suggestions will be appreciated.
(j.l. edited subject)
[ October 14, 2002: Message edited by: Junilu Lacar ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Your subject says you are writing to an input stream -- did you mean an output stream? This may or may not be relevant to your problem.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
oops!!! That's a typo. I mean FileOutputStream.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Ronnie,
It's hard to judge without seeing the code, but you may not be resetting your filepointer properly.
All best,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
[ October 14, 2002: Message edited by: Max Habibi ]

Java Regular Expressions
Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
Max,
I'll check my file pointer and see if that's the problem. If I'm setting my file pointer wrong, shouldn't it just write at the wrong position. Why would there be garbage at the header?
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Originally posted by Ronnie Phelps:
Max,
I'll check my file pointer and see if that's the problem. If I'm setting my file pointer wrong, shouldn't it just write at the wrong position. Why would there be garbage at the header?

Well, you might be pointing at the header. You might also be opening in append mode, etc.
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
I don't know what's up with the FileOutputStream. I had to specify a mode before the size of the output stream changed from zero. But either way it looked as though it was appending whether I set append mode or not.
Anyway, I used a RandomAccessFile and everything works okay.
By the way I'm still not done with this beta exam. I don't plan to pass but at least I know that the exam is in arms reach now and when I pay for the exam my company will reimburse me anyway. So I'm gonna finish up in the next two hours just so I can have the oppurtunity take the follow up exam and use it as a learning experience.

Thanks again max.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem Writing to FileOutputStream