Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

very confused with BufferedOutputStream

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 9625
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
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.
 
Montana has cold dark nights. Perfect for the heat from incandescent light. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic