Note that there are some explanatory texts on larger screens.

plurals
  1. POFileNotFound (Access is denied) Exception on java.io
    primarykey
    data
    text
    <p>Why do I get this error when I run this program? This occurs after random iterations. Usually after the 8000th iteration.</p> <pre><code>public static void main(String[] args) { FileWriter writer = null; try { for(int i = 0; i &lt; 10000; i++) { File file = new File("C:\\Users\\varun.achar\\Desktop\\TODO.txt"); if(file.exists()) { System.out.println("File exists"); } writer = new FileWriter(file, true); writer.write(i); System.out.println(i); writer.close(); if(!file.delete()) { System.out.println("unable to delete"); } //Thread.sleep(10); //writer = null; //System.gc(); } } catch(IOException e) { e.printStackTrace(); } finally { if(writer != null) { try { writer.close(); } catch(IOException e) { e.printStackTrace(); } } } } </code></pre> <p>After the exception occurs, the file isn't present. That means the it is deleting, but FIleWriter tries to acquire the lock before that, even though it isn't a multi threaded program. Is it because the Windows isn't deleting the file fast enough, and hence the FileWriter doesn't get a lock? If so, then file.delete() method returns before windows actually deletes it?</p> <p>How do i resolve it, since i'm getting a similar issue during load testing my application.</p> <p>EDIT 1: Stacktrace:</p> <pre><code>java.io.FileNotFoundException: C:\Users\varun.achar\Desktop\TODO.txt (Access is denied) at java.io.FileOutputStream.openAppend(Native Method) at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:192) at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:116) at java.io.FileWriter.&lt;init&gt;(FileWriter.java:61) </code></pre> <p><strong>EDIT 2</strong> : Added file.exists() and file.delete conditions in the program. and the new stacktrace:</p> <pre><code>7452 java.io.FileNotFoundException: C:\Users\varun.achar\Desktop\TODO.txt (Access is denied) at java.io.FileOutputStream.openAppend(Native Method) at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:192) at java.io.FileWriter.&lt;init&gt;(FileWriter.java:90) at com.TestClass.main(TestClass.java:25) </code></pre> <p><strong>EDIT 3</strong> Thread dump</p> <pre><code>TestClass [Java Application] com.TestClass at localhost:57843 Thread [main] (Suspended (exception FileNotFoundException)) FileOutputStream.&lt;init&gt;(File, boolean) line: 192 FileWriter.&lt;init&gt;(File, boolean) line: 90 TestClass.main(String[]) line: 24 C:\Users\varun.achar\Documents\Softwares\Java JDK\JDK 6.26\jdk\jre\bin\javaw.exe (09-Nov-2011 11:57:34 PM) </code></pre> <p><strong>EDIT 4</strong> : Program runs successfully on different machine with same OS. Now how do i ensure that the app with run successfully in the machine it is deployed in?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload