views:

29

answers:

2

I m accessing file from java application, but i m getting following exception

c:\Program Files\sample\sample\cample.xml (Acc?s refus?)        
at java.io.FileOutputStream.(Unknown Source)  
at java.io.FileOutputStream.  
at java.awt.event.InvocationEvent.dispatch(Unknown Source)  
at java.awt.EventQueue.dispatchEvent(Unknown Source)    
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)  
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)  
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)  
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)  
at java.awt.EventDispatchThread.run(Unknown Source)

is any body have any idea why this exception occurred .....,solution for this..... is it related to file permission means file have not read permission.

+1  A: 

This exception means that the account you are running your application under doesn't have write permissions to the file. You will need to grant the necessary permissions to the file you are trying to write to. Another option is to put the file into another folder where you you have the necessary permissions.

Darin Dimitrov
Or to the directory, in order to create the file. Are you sure you want "cample.xml" (not "sample.xml")?
Thilo
if user have created a file at this location then is it correct that we move this file programmatic to another location like temp, %APPDATA%.
raji
@raji: a user cannot create a file in that location, because he receives `Accès Refusé`. But when the error occurs, you can suggest the user another location (like `%APPDATA%` indeed, see also my take on the issue).
Abel
+2  A: 

With my limited knowledge of French, I think "Acc?s refus?" is, on your system, "Accès refusé?" or something similar. Which means: "access denied". In other words, your Java program is not allowed to go to the location you mention.

Considering that the location is under c:\program files, this location is indeed forbidden for Windows Vista and later. It is better to choose a location under %APPDATA% or elsewhere where you have writing access.

Note that Windows tries to prevent writing to delicate locations for security reasons. Even if you gain the right by changing your account to Administrator, you still should not put data, settings, files etc under Program Files. Instead, it should go under the current user's, or the default user's application data directory. In Java you can get this path as follows:

// get application path
System.getenv("APPDATA");
Abel
The location "Program Files" could indeed be the problem. I once made the mistake of installing an apache web server below "Program Files". Since then it's *always* a challenge to apply changes to `httpd.conf`, even with a simple text editor...
Andreas_D
@Andreas_D: two hints on Apache on Windows: (1) rightclick your editor (even Notepad) and select *Run As Administrator*, (2) change the location of `httpd.conf` (must be done in registry or batch file for commandline). I use the latter to much satisfaction (never liked httpd.conf next to the executable anyway).
Abel