This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
I was curious about the wording of the documentation for a few classes in the java.io 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).
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!
Joined: Dec 10, 2001
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.
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 ]
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?