Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Close and BufferWriter

Reply
Thread Tools

Close and BufferWriter

 
 
Berlin Brown
Guest
Posts: n/a
 
      09-07-2004
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();

 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      09-07-2004
Berlin Brown wrote:

> 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().
 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      09-07-2004

"Michael Borgwardt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Berlin Brown wrote:
>
>> 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().


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.)


 
Reply With Quote
 
Filip Larsen
Guest
Posts: n/a
 
      09-07-2004
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,
--
Filip Larsen


 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      09-08-2004
Filip Larsen wrote:

> Berlin Brown wrote


>>fileWriter = new FileWriter(_file);
>>outputFile = new BufferedWriter(fileWriter);
>>PrintWriter out = new PrintWriter(outputFile);
>>
>>outputFile.close(); // ???
>>out.close(); // ???


> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to close a TCP socket? (TCPSocket#close doesn't close it) IƱaki Baz Castillo Ruby 7 01-12-2010 01:32 PM
Browser close event (MSIE and FF): how to detect browser close ? pamelafluente@libero.it Javascript 0 01-23-2007 12:44 AM
Why does JdbcRowSetImpl.close() close the database connection? Paul van Rossem Java 0 04-07-2005 07:01 PM
JavaMail POP3 folder.close() method close also store! Daniel Albisser Java 1 04-07-2004 03:45 PM
How to close child browser while parent close? Denon ASP .Net 1 11-14-2003 08:14 AM



Advertisments