title: Java file IO and "access denied" errors
body: <p>I have been tearing my hair out on this and thus I am looks for some help . </p>
  
  <p>I have a loop of code that performs the following </p>
  
  <pre><code>//imports ommitted
  public void afterPropertiesSet() throws Exception{
  
    //building of URL list ommitted  
    // urlMap is a HashMap &lt;String,String&gt; created and populated just prior   
  
      for ( Object urlVar : urlMap.keySet() ){
      String myURLvar = urlMap.get(urlVar.toString);
      System.out.println ("URL is "+myURLvar );
      BufferedImage imageVar = ImageIO.read(myURLvar);//URL confirmed to be valid even for executions that fail
      String fileName2Save = "filepath"// a valid file path
      System.out.println ("Target path is "+fileName2Save );
      File file2Save = new File (fileName2Save);
      fileName2Save.SetWriteable(true);//set these just to be sure
      fileName2Save.SetReadable(true);
        try{
         ImageIO.write (imageVar,"png",file2save)//error thrown here 
        }catch (Exception e){
       System.out.println("R: "+file2Save.canRead()+" W: "+file2Save.canWrite()+" E:"+file2Save.canExecute()+" Exists: "+file2Save.exists+" is a file"+file2Save.isFile() );
  
       System.out.println("parent Directory perms");// same as above except on parent directory of destination
        }//end try
       }//end for
       }
  </code></pre>
  
  <p>This all runs on Windows 7 and JDK 1.6.26 and Netbeans,Tomcat 7.0.14 . The target directory is actually inside my netbeans project directory in a folder for a normal web app ( outside WEB-INF) where I would expect normally to have permission to write files. </p>
  
  <p>When the error occurs I get one of two results for the file a.) All false b.)all true. The Parent directory permission never change all true except for isFile.</p>
  
  <p>The error thrown  ( java.IO.error  with "access denied" ") does not occur every time ... in fact 60% of the time the loop runs it throws no error. The remaining 40% of the time I get the error on 1 of the 60+ files it writes. Infrequently the same one. The order in which the URLs it starts from changes everytime so the order in which the files are written is variable. The file names have short concise names like "1.png". The images are small..less then 8k.</p>
  
  <p>In order to make sure the permissions are correct I have :</p>
  
  <p>Given "full control" to EVERYONE from the net beans project directory down</p>
  
  <p>Run the JDK,JRE and Netbeans as Administrator</p>
  
  <p>Disabled UAC</p>
  
  <p>Yet the error persists. Google searches for this seem to run the gamut and often read like vodoo. Clearly I ( and Java and Netbeans etc )  should have permission to write a file to the directory .</p>
  
  <p>Anyone have any insight ? This is all ( code and the web server hosting the URL) on a closed system so I can't cut and paste code or stacktrace.</p>
  
  <p>Update: I confirmed the imageURL is valid by doing a println &amp; toString prior to each read. I then confirmed that a.) the web server hosting the target URL returned the image with a http 200 code b.) that the URL returned the image when tested in a web browser. In testing I also put a if () in after the read to confirm that the values was not NULL or empty.  I also put in tests for NULL on all the other values . They are always as expected even for a failure .The error always occurs inside the try block. The destination directory is the same every execution. Prior to every execution the directory is empty.</p>
  
  <p>Update 2: Here is one of the stack traces ( in this case perms for file2Save are R: True W:True E: True isFile:True exists:True )</p>
  
  <pre><code>    java.io.FileNotFoundException &lt;fullFilepathhere&gt; (Access is denied)
         at java.io.RandomAccessFile.open(Native Method)
         at java.io.RandomAccessFile.&lt;init&gt;(RandomAccessFile.java:212)
         at javax.imageio.stream.FileImageOutputStream.&lt;init&gt;(FileImageOutputStream.java:53)
         at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(FileImageOutputStreamSpi.java:37)
         at javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:393)
         at javax.imageio.ImageIO.write(ImageIO.java:1514)
         at myPackage.myClass.afterPropertiesSet(thisClassexample.java:204)// 204 is the line number of the ImageIO write
  </code></pre>