• Post Reply Bookmark Topic Watch Topic
  • New Topic

very confused with BufferedOutputStream

 
Bob Peterson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming I'm mis-understanding how BufferedOutputStream is supposed to work. What I'm trying to do is write to an OutputStream, but not actually write to the underlying file until I flush/close it in a separate operation.

I'm stepping through it, and it appears that the file is created in the first line below, with the new FileOutputStream(fileName).

And later, when I flush and close the OS, it doesn't actually write the data. So what I am left with is an empty file.

What's going on?

Here's my code:

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got two different buffers going here: one in the BufferedWriter, the other in the BufferedOutputStream. The data can get stuck in either buffer until the whole entire stack is flushed: in your example, that would mean flushing the BufferedWriter. Furthermore, there's no guarantee the OS is going to actually put bits onto the disk until you close the disk file, so you might as well just call close() on the BufferedWriter, which will implicitly flush everything along the way.

That said, you've got way too many objects involved here! If you want to write to a file using a BufferedWriter, just say

BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));

and when you're through, close "writer", and Bob's your uncle.
 
Bob Peterson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. If I was on my own, I would have done it the way you recommended. However, the reason I'm doing this screwy way is that I'm working from a design given to me, and it was detailed enough to indicate that we're supposed to write to OutputStreams as a kind of buffer, and then flush/close the stream to the file when a different method (commit()) is called.

I'm trying to find out right now if and how much we can deviate from the design.

So, back to the original question. Is it possible to remporarily buffer data in an OutputStream w/out immediately writing to the file?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15715
73
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the buffer in BufferedOutputStream was only meant to help performance - writing one larger block of data to a disk is much faster than writing lots of small pieces of data.

BufferedOutputStream, nor any of the other I/O classes, was not designed to help you control exactly when the buffer is flushed to disk. So you can't rely on that mechanism to solve your problem.

You could write the data to a temporary file, and when commit() is called you rename the temp file to the definitive file, and in rollback() you delete the temp file.
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also write your data to a ByteArrayOutputStream, which uses an in-memory array (provided there's not TOO much data). Then when you are done, just write the contents of that array to disk.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!