Forums Register Login

FileWriter experience

+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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?
Did you just should on me? You should read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1156 times.
Similar Threads
The FileWriter Class
Do FileWriter objects create a file if the specified file does not exist?
PrintWriter please help
File q, help requested!
Will FileReader create a new file?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:27:44.