File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes FileWriter experience Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "FileWriter experience" Watch "FileWriter experience" New topic

FileWriter experience

Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
I was curious about the wording of the documentation for a few classes in the package. I researched the subjects of my curiousity and here is what I learned (should anybody be interested):
According to the documentation for the FileWriter class concerning the constructor that takes a String as its argument, it should throw an IOException "if the specified file is not found." However, considering this example:

if the specified file out.txt does not exist, then the file is simply created.
The source code for the FileWriter class indicates that this constructor in fact constructs a new FileOutputStream using the specified String and then invokes its super's (OutputStreamWriter) constructor that takes a FileOutputStream. FileOutputStream uses this String to construct a new File object. If a new File object is created with a String that specifies a path that does not exist, then the file or path that the File object specifies does not exist which can be verified with the File::exists() method:

This displays false as would be expected. Following the aforementioned chain of constructors one step forwards (or backwards - whatever conceptualization is preferred), this:

displays true which is in accordance with the documentation for the associated FileOutputStream constructor which states that a FileNotFoundException is thrown "if the file ... does not exist but cannot be created." In this example the file can be created and so it is. The FileWriter constructor then uses this newly (and indirectly) created file and everyone is happy (except the guy who reads the documentation for the first time).

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Looking at the 1.4 documentation, it states:
Whether or not a file is available or may be created depends upon the underlying platform.

So apparently there may be cases where a file can not be created, and so you would get the IOException. Haven't ever seen this happen myself, but it seems they added this little tidbit in there, probably to reconcile the confusion over the previous documentation.
Thanks for the info on how FileWriter goes about creating itself, never looked at the source code for it!
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
For the sake of greater completion, the chain of constructors in FileOutputStream eventually invokes open( String ) which is a native method and I've not much ambition to inspect native code anymore. The dirty work of creating the file if necessary is apparently done in this native method which does throw a FileNotFoundException presumably if the file does not exist and cannot be created.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
So apparently there may be cases where a file can not be created, and so you would get the IOException.
As an example, the file-to-be might be in a read-only directory. Or the File may represent a path containing some directories which don't exist. In the latter case, you need to create parent directories before creating subdirectories or files.
[ May 19, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
What are the arguments that are passed for the OutputStreamWriter in the case of intantiating the FileWriter?
One argument is obviously the FileOutputStream.
Is the other argument the encoding format of the underlying operating system?

Help me!Help you!!!
I agree. Here's the link:
subject: FileWriter experience
It's not a secret anymore!