• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HELP!!! Output stream closing prematurely

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am having problems with the class defined below. The output stream is being closed with out me closing it. It happens when I go to write something out. I have now added code to open the stream before I write even if the stream is thought to be open and it still is closed sometimes. Here is the class below. Any help is greatly appreciated.
<code>
public class FileOutputWriter
{
// The PrintWriter for writing to text files
private BufferedWriter bufferedWriter = null;
private FileWriter fileWriter = null;
// The OutputState of this object
private OutputWriterState state = null;
PrintWriter out = null;
boolean streamIsOpen = false;
String outputTarget = null;
public FileOutputWriter(String outputTarget)
{
this.outputTarget = outputTarget;
}

synchronized public boolean open()
throws IOException
{
boolean append = false;
// Check the state of this object
if (!state.equals(OutputWriterState.NEW))
append = true;

File fout = new File (outputTarget);

fileWriter
= new FileWriter (fout.getAbsolutePath(), append);
bufferedWriter = new BufferedWriter (fileWriter);
out = new PrintWriter (bufferedWriter);
streamIsOpen = true;
return true;
}
synchronized public void close()
throws IOException
{
streamIsOpen = false;
bufferedWriter.close();
out.close();
fileWriter.close();
}

synchronized public boolean write(String rec)
throws IOException
{
open();
out.print (rec);
out.flush (rec);
}
} // class
</code>
Please help. This is driving me nuts!
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could it be that you don't verify the streamIsOpen before trying to write?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is that your problem is here:
<pre>
bufferedWriter.close();
out.close();
fileWriter.close();
</pre>
When you close a higer-level stream, it automatically flushes and then closes the lower-level stream inside it. And when you flush a higher-level stream, it flushes the lower-level stream inside. So, when you call out.close(), it calls out.flush(), which calls bufferedWriter.flush(), which would then call fileWriter.flush() and fileWriter.close(). Then out.close() would also invoke bufferedWriter.close() before it completes, which would re-flush everything and then invoke fileWriter.close(). So everything would be flushed and closed by the one out.close(). Except] that you've already closed bufferedWriter beforehand, so when you get to the bufferedWriter.flush(), you get an IOException.
So, you don't really need to close the lower-level streams, as they will be taken care of anyway. But if you want to close them anyway to be sure, you can only cose them after the higher-level streams are closed. It's not a problem if a given stream is closed twice - it's just a problem if it's flushed after it's closed. So just close your streams in the inverse order that they were opened in:
<pre>
out.close();
bufferedWriter.close();
fileWriter.close();
</pre>
Aside from this, you may also have problems because for some reason write() opens a new stream whenever it's invoked, but does not close it. What if it's already open? Wouldn't it better to check if the file is already open, and if so, re-use the existing connection, rather than dropping it and creating a new one? Or, if you really want to open a new writer on each write, you should probably make sure that any previous writer is closed first. I'm not sure what your goal is here, so I'll leave it at that.
 
Sgt. John Seifert
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually i put the <code>open()</code> call in write because i was having a problem with the stream closing. it was sort of a work around to the already existing problem. Typically i would write a 1000 or so lines to a file without closing or re-opening the stream.
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic