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



...
1) ** USING BUFFERED WRITER - EXAMPLE FROM JAVA.SUN.COM FOR Java 1.2 **

File fileOut = new File("/space/users/swd/java/testFile.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(fileOut));
out.write("we are writting to a file ");
out.close();

2) ** USE FILEWRITER AS I ORIGINALLY POSTED WITH HELP FROM ABOVE **

File fileOut = new File("/space/users/swd/java/testFile.txt");
FileWriter fw = new FileWriter(fileOut);
fw.write("docHeader\n");
fw.close();

IOW should I use the BufferedWriter wrapper or not?

It has already been said here several times: the BufferedWriter is better in almost all cases. I'd like to add that you may like to consider this:

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

The PrintWriter interface is very convenient in many cases, allowing you to print or println all the built-in data types. Depending on what you really want to write in the end, this may be nicer.

You have to read the section in the documentation about error handling, though. It is different to other writers (no exceptions).

Michael
--
Michael Kvlling School of Network Computing
http://www.netcomp.monash.edu.au/~mik Monash University




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.