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



Thanks so much for the help.

> You'll want to fw.close() or fw.flush() the file. Otherwise the
> output will be buffered until the object is finalized (most output
> stream objects close themselves on finalization -- since you
> obviously forgot). But since your application probably just
> terminates immediately, it's most likely that none of your objects
> are ever finalized, and thus your file is never closed.

That did the trick - I am sure the other sample sent in would work as
well. Now my question is which way is better of the following two
methodologies: (most code snipped for brevity)

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?

> You might also want to dump the exception or the stack trace in your
> catch clause, otherwise you really have no idea what happened.
Gotcha.

Thanks again,
Jon


References: 
 >Re: Writing to a file (From: James Bucanek <email@hidden>)



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.