title: Checking for write access in a directory before creating files inside it
body: <p>My small utility application asks the user for an output directory via a GUI file selector.
  Then it creates a lot of files in this output directory after some processing.</p>
  
  <p>I need to check if the application has write access so that it informs the user and does 
  not continue with the processing (which might take a long time)</p>
  
  <p>My first attempt was the <strong>canWrite()</strong> method of <strong>java.io.File</strong>. But this does not work
  since it deals with the directory entry itself and not its contents. I have seen at least
  one instance of a Windows XP folder that can be renamed or deleted but no files may be created
  in it (because of permissions). This is actually my testcase.</p>
  
  <p>I finally settled with the following solution</p>
  
  <pre><code>//User places the input file in a directory and selects it from the GUI
  //All output files will be created in the directory that contains the input file
  File fileBrowse = chooser.getSelectedFile(); //chooser is a JFileChooser
  File sample = new File(fileBrowse.getParent(),"empty.txt"); 
  try
  {
       /*
        * Create and delete a dummy file in order to check file permissions. Maybe 
        * there is a safer way for this check.
        */
        sample.createNewFile();
        sample.delete();
  }
  catch(IOException e)
  {
        //Error message shown to user. Operation is aborted
  }
  </code></pre>
  
  <p>However this does not feel elegant to me since it just tries to actually create a file and checks if the operation succeeds.</p>
  
  <p>I suspect that there must be a better way for this but all solutions I have found so far
  with Security Managers and stuff deal with Java Applets and not standalone applications.
  Am I missing something?</p>
  
  <p>What is the recommended way of checking for file access inside a directory before
  actually writing the files?</p>
  
  <p>I am using Java 5.</p>