Creating a PrintWriter with BufferedWriter versus just File

K

KevinSimonson

What's the difference between this code:

PrintWriter destination
= new PrintWriter
( new BufferedWriter( new FileWriter( new File( arg))));

and this code:

PrintWriter destination = new PrintWriter( new File( arg));

Do I get buffered output with the first code that I don't get with the
second code? Or do I get the same buffered behavior with both?

Kevin Simonson
 
L

Lew

KevinSimonson said:
What's the difference between this code:

PrintWriter destination
= new PrintWriter
( new BufferedWriter( new FileWriter( new File( arg))));

and this code:

PrintWriter destination = new PrintWriter( new File( arg));

Do I get buffered output with the first code that I don't get with the
second code? Or do I get the same buffered behavior with both?

No, the write semantics differ between the two. In the BufferedWriter version, writes to the underlying stream occur only when the BufferedWriter buffer fills. With the second form, writes occur every time the PrintWriter writes. There is nothing in the documentation of PrintWriter
<http://download.oracle.com/javase/7/docs/api/java/io/PrintWriter.html>
to indicate that it buffers writes. So it seems unlikely that you would "get the same buffered behavior with both".
 
A

Arne Vajhøj

What's the difference between this code:

PrintWriter destination
= new PrintWriter
( new BufferedWriter( new FileWriter( new File( arg))));

and this code:

PrintWriter destination = new PrintWriter( new File( arg));

Do I get buffered output with the first code that I don't get with the
second code? Or do I get the same buffered behavior with both?

You can lookup the classes in the Java docs.

If the docs does not say that class XYZ does ABC, then
you should not rely on class XYZ doing ABC even though
a specific implementation may do it.

Arne
 
R

Roedy Green

What's the difference between this code:

PrintWriter destination
= new PrintWriter
( new BufferedWriter( new FileWriter( new File( arg))));

and this code:

PrintWriter destination = new PrintWriter( new File( arg));

Do I get buffered output with the first code that I don't get with the
second code? Or do I get the same buffered behavior with both?

You don't get buffering, or at least significant buffering. Given that
computers have at least four times as much RAM as they had when Java
io was conceived, you probably want to use 64K buffers, or read the
whole file in one fell swoop, when the file does not stay open.
see http://mindprod.com/products1.html#HUNKIO

See http://mindprod.com/applet/fileio.html
to generate you sample code for Java I/O.
--
Roedy Green Canadian Mind Products
http://mindprod.com
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.
 
A

Arne Vajhøj

You don't get buffering, or at least significant buffering. Given that
computers have at least four times as much RAM as they had when Java
io was conceived,

4??

More than 40!
you probably want to use 64K buffers,

64 KB buffer was not a problem when java.io was invented.

Arne
 

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,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top