• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Do FileWriter objects create a file if the specified file does not exist?

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do FileWriter objects create a file if the specified file does not exist? The API doc says it throws an IOException, but when I tried it (using the FileWriter(String fileName) constructor) it created a file! What's up with that!?
FileWriter
public FileWriter(String fileName)
throws IOException
Constructs a FileWriter object given a file name.
Parameters:
fileName - String The system-dependent filename.
Throws:
IOException - if the specified file is not found or if some other I/O error occurs.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tricky isn't?
In a case like this, go look at the sources...
here goes:
FileWriter is a subclass of OutputStreamWriter. All it does when you build a new FileWriter is calling super with a FileOutputStream built on the file you provided.

Now, when a FileOutputStream is created it may throw FileNotFoundException (subclass of IOException) in case the filename you supplied was the name of a directory (which cannot be written to). Further, OutputStreamWriter may throw an UnsupportedEncodingException (subclass of IOException too) in case a wrong encoding has been supplied.
So in order to throw both FileNotFoundException and UnsupportedEncodingException, FileWriter has to declare IOException (the superclass) in its throw clause...
try giving in a wrong encoding or the name of a directory...
HIH
 
Bob Graffagnino
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the impression from the API doc that if the file name you supplied the FileWriter constructor was not a name of a currently existing file, the constructor would throw an IOException. In other words, FileWriter will not create new files.
This does not appear to be true when I code and run an example. The FileWriter object that I instanciate actually creates a file when the file name I give the constructor is not one of an existing file.
In other words, can FileWriter objects create files on the file system? (Appartently YES!)
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most, if not all, of the I/O commands that deal with files will try to create one if it doesn't already exist when you actually try writing to it.

Rob
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it does because a FileWriter is just a Wrapper around a FileInputStream providing an encoding for writing to the file. It's the FileOutputStream (the one created in the constructor of FileWriter) that creates the File on the filesystem if necessary (in case it does not exist), if authorized (SecurityManager allows it) and if not a directory (because you can't write to a directory).
HIH
 
reply
    Bookmark Topic Watch Topic
  • New Topic