FileNotFound (access denied) Exception on java.io

Why am I getting this error when running this program? This happens after random iterations. Usually after the 8000th iteration.

public static void main(String[] args) { FileWriter writer = null; try { for(int i = 0; i < 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(); } } } } 

After an exception occurs, the file is missing. This means that it is being deleted, but FIleWriter is trying to get a lock before that, although it is not a multithreaded program. Is this because Windows does not delete the file fast enough, and therefore FileWriter does not get the lock? If so, does the file.delete () method return before the windows actually deleted it?

How can I solve it, since a similar problem occurs during the loading of my application.

EDIT 1: Stacktrace:

 java.io.FileNotFoundException: C:\Users\varun.achar\Desktop\TODO.txt (Access is denied) at java.io.FileOutputStream.openAppend(Native Method) at java.io.FileOutputStream.<init>(FileOutputStream.java:192) at java.io.FileOutputStream.<init>(FileOutputStream.java:116) at java.io.FileWriter.<init>(FileWriter.java:61) 

EDIT 2 : added conditions file.exists () and file.delete in the program. and a new stack:

 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.<init>(FileOutputStream.java:192) at java.io.FileWriter.<init>(FileWriter.java:90) at com.TestClass.main(TestClass.java:25) 

EDIT 3 Dump stream

 TestClass [Java Application] com.TestClass at localhost:57843 Thread [main] (Suspended (exception FileNotFoundException)) FileOutputStream.<init>(File, boolean) line: 192 FileWriter.<init>(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) 

EDIT 4 : the program runs successfully on another computer with the same OS. Now, how can I make sure that the application runs successfully on the machine in which it is deployed?

+7
source share
9 answers

Thanks guys for helping me, but here's how it was finally resolved.

 public static void main(String[] args) { FileWriter writer = null; try { for(int i = 0; i < 10000; i++) { File file = new File("C:\\tenant-system-data\\abc.txt"); if(!file.getParentFile().canWrite()) { System.out.println("parent file error"); } if(file.exists()) { System.out.println("File exists"); } int count = 0; while(count++ < 5) { try { file.createNewFile(); break; } catch(IOException e) { try { Thread.sleep(100); } catch(InterruptedException e1) { e1.printStackTrace(); } } } 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(); } } } } 
0
source

On any OS, you can only have a certain number of open files / streams on a stretch. It seems you are getting into the OS limit. Try setting the file to zero inside the loop.

+4
source

If I understand your stack trace correctly, an exception is thrown when trying to create a new FileWriter . It is impossible to find out the reason without exploring a little further.

  • The return values โ€‹โ€‹may say something. In particular, check that File.delete() returns.
  • Before attempting to create a new FileWriter , verify that File.exists() returns.

If the previous delete() returns true and exists() right after it also returns true in a single-threaded program, then this is really something strange.

Edit:, so it seems that the deletion was successful, and after that the file did not exist. Of course, this should work, so it is strange why FileWriter throws an exception. Another thought, try checking out File.getParentFile().canWrite() . That is, your permissions to write to the directory somehow disappear.

Edit 2:

Do not get an error on another machine with the same OS. Now, how can I make sure that this error does not appear in the application, where will it be deployed?

So far you have one machine that does not work correctly and works correctly. Perhaps you could try it on cars. It is possible that the first machine is somehow broken and causes errors. It's amazing how often digital computers and their programs (I mean OS and Java, not necessarily your program) can simply be โ€œa little brokenโ€, so they almost always work perfectly, but randomly do not work with certain equipment and used the case โ€” usually under heavy load โ€” is similar to how incorrect multi-threaded programs may work. This should not be your mistake to be your problem :-)

Frankly, the only way to make sure that errors do not occur on machine X is to run the program on machine X. Unusual things, such as creating and deleting the same file 8,000 times in a row, are error prone, even if it should work . Computers, operating systems, and APIs are not perfect. The more unusual material you make, the more often imperfections become aware of themselves, because unusual use is usually less thoroughly checked than everyday operations.

+2
source

I had the same problem: an open, remote Java program (single-threaded) that opens the same file again and again.

On some Windows systems, we get the same problem that is reported here; on Linux, Solaris, and other Windows systems, it works fine.

Tracking the program using SysInternals Process Monitor (now MS) it is clear that the removal is performed first at the OS level and cleans the subsequent open does not work with the status PENDING DELETE.

Thus, at the OS / NTFS / Disk level, there seems to be a slight delay before the file is actually deleted, and this, apparently, is the reason for the accidental failure in our case.

As a workaround, I changed the .delete () call instead, just write on top of its new FileWriter file (file) and it seems to work.

The problem did not occur on all systems, one particular model that always fails, and all this after an unlimited number of cycles is Windows 7 / Dell Lattitude E6420 with WD Smartdrive, while my accuracy is for Windows 7 / Dell M4600 (with solid state drive) ) or T3400 with Linux I never had a problem.

Cheers - Mark

+2
source

Can you conditionally try to write a file?

Use file.exists and then write to it so that you can avoid any other problems. It is hard to say from this exception.

http://download.oracle.com/javase/6/docs/api/java/io/File.html#exists ()

Could you also post a stream dump at this point, just to debug it further.

Please flush the author before writing again.

+1
source

It may be a long shot, but you can try to work with a file that is NOT located directly on the desktop. Instead:

 "C:\\Users\\varun.achar\\Desktop\\TODO.txt" 

Try:

 "C:\\Users\\varun.achar\\SomeOtherDirectory\\TODO.txt" 

The OS can kill you here with all the hooks for the desktop ...

EDIT based on comments:

  • Are there any scheduled tasks on a bad machine?
  • Instead of debugging the environment, do you have a sys administrator for this?
  • Does it work on a clean install of Windows? [95% chance it will be]
  • Since the main reason, apparently, is the environment and not the solution to the Windows configuration problem, can you move forward with other tasks and leave it to someone who will keep a list of discrepancies between the systems?
+1
source

These are the scripts that you must process before deleting the file http://www.java2s.com/Code/Java/File-Input-Output/DeletefileusingJavaIOAPI.htm

at least check for the return value in your program.

0
source

I had the same issue (FileWriter and Access Denied).

My guess for a reason: Windows 7 blocked the file because a preview of the contents of the file was shown in the explorer window (the file was selected in the window).

Solution: I deleted the file in the explorer window. And the IO exception failed.

0
source

You have permission to delete in the directory, but do not create permission.

-2
source

All Articles