Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Writing to a file



=?iso-8859-1?Q?Michael_K=F6lling?= <email@hidden> wrote:

>PrintWriter out = new PrintWriter(new BufferedWriter(new
>FileWriter(fileOut)));
>out.print("we are writting to a file ");

Be careful with PrintWriters. You can defeat yourself, or at least hobble
yourself, if you don't look at things carefully.

The two PrintWriter constructors that take an OutputStream arg:
public PrintWriter(OutputStream out)
public PrintWriter(OutputStream out, boolean autoFlush)

both ALWAYS put a BufferedWriter between the PrintWriter, the
OutputStreamWriter, and the OutputStream. Extra buffering is not usually a
serious performance hit, but it can eat up memory like crazy.

Also, be sure when you make the PrintWriter that you have autoFlush OFF
unless you have a very good reason to turn it on. Otherwise, every time
you call println(), you'll flush the buffer. Net result: any speed that
could be gained by buffering is discarded.

-- GG




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.