Close and BufferWriter

B

Berlin Brown

This is basic java, and I have an idea, but without looking at the java
code, I cant get an 100% understanding.

File _file = new File("somefilename");
FileWriter fileWriter = null;
BufferedWriter outputFile = null

fileWriter = new FileWriter(_file);
outputFile = new BufferedWriter(fileWriter);
PrintWriter out = new PrintWriter(outputFile);

outputFile.close(); // ???
out.close(); // ???


In this close what would physically close the file.
outputFile.close() ...which is the BufferedWriter or
out.close();
 
M

Michael Borgwardt

Berlin said:
This is basic java, and I have an idea, but without looking at the java
code, I cant get an 100% understanding.

File _file = new File("somefilename");
FileWriter fileWriter = null;
BufferedWriter outputFile = null

fileWriter = new FileWriter(_file);
outputFile = new BufferedWriter(fileWriter);
PrintWriter out = new PrintWriter(outputFile);

outputFile.close(); // ???
out.close(); // ???


In this close what would physically close the file.
outputFile.close() ...which is the BufferedWriter or
out.close();

Both, becaause all stream, writer and reader classes call the
underlying stream/writer/reader's close() in their close()
method. It is therefore sufficient (and advisable) to call the
outermost stream/writer/reader's close().
 
M

Mike Schilling

Michael Borgwardt said:
Both, becaause all stream, writer and reader classes call the
underlying stream/writer/reader's close() in their close()
method. It is therefore sufficient (and advisable) to call the
outermost stream/writer/reader's close().

Conversely, if you're done with an output filter but don't want to close the
underlying stream, just flush() the filter. (There's no similar solution
with input filters, since there's no general way to push the stuff buffered
in the filter back onto the input stream.)
 
F

Filip Larsen

Berlin Brown wrote
File _file = new File("somefilename");
FileWriter fileWriter = null;
BufferedWriter outputFile = null

fileWriter = new FileWriter(_file);
outputFile = new BufferedWriter(fileWriter);
PrintWriter out = new PrintWriter(outputFile);

outputFile.close(); // ???
out.close(); // ???

In this close what would physically close the file.
outputFile.close() ...which is the BufferedWriter or
out.close();

The typically use is to close the outermost Writer in the chain, which
in your case is the BufferedWriter. It will then flush its buffers and
propagate close to the next Writer in the chain. There is really no need
to close the FileWriter explicitely, but if you do you should do it
*after* you have closed or at least flushed the BufferedWriter.


Regards,
 
J

John C. Bollinger

Filip said:
Berlin Brown wrote
The typically use is to close the outermost Writer in the chain, which
in your case is the BufferedWriter.

Look a bit closer: in his case the outermost Writer is the PrintWriter.
Just pointing it out in hopes of avoiding confusion.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top