Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt regarding file handling

 
abin joy
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File file=new File("myfile.txt");

file.createNewFile()
file.isDirectory()

Here file.createNewFile() needs a try/catch block or need throws declaration in the method signature to compile where as file.isDirectory() is just fine.I expect file.isDirectory() to behave the same way as file.createNewFile() behaves.Can anyone explain this
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would it throw an IOException if no I/O takes place?
The method can throw an unchecked SecurityException, which (obviously) is not subject to the same catch-or-declare requirement as the IOException thrown by the createNewFile() method.
 
Nickolas Case
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
file.createNewFile is modifying the file system while file.isDirectory is only checking the status of the file system.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic