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!