Creating file on Windows 7 causes “Access denied” exception

北城以北 提交于 2019-11-26 21:56:33

问题


I have a Java application were the user can create a text file and save it wherever he wants on his computer using this code :

File txtFile = new File( path );
Writer writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( txtFile ), "UTF-8" ) );  // Error occurs here.

But many users using Windows 7 reported that when saving the file to "C:\", they get "Access is denied" error. I found that this is because they need administrator permissions to save the file in such path in Win7.

Instead of showing a warning message to the user: " You can't save the file at this path ", can i save the file in this path somehow, like if there is a way to have Administrator permissions in Win7 through Java code, or something like that ?


回答1:


Short answer - no.

If you need to save to C drive, they need permissions. If this program just needs to create files, you can use the users temp folder. See System.getProperty()




回答2:


Windows Vista and Windows 7 have UAC enabled. UAC denies creating new files in SOME locations, without administrative privileges.

Check your permissions and make sure to execute the java executable in ADMINISTRATIVE Account, OR disable UAC.

To do that, go to "Start" type in "CMD.EXE" -> right click on the cmd.exe file and Run As Administrator. Then navigate to the location containing the .class file. Then type in java ClassFile and hit enter




回答3:


Are you using cmd , i.e. Dos to run your file or eclipse? Whatever you are using It looks like you are running as default user. in windows 7, UAC by default blocls writimg to system.directory.

Do the following and hopefully it should work1

> If cmd.exe
>      Then when you open run from start menu. Right click it, select run as administrator and  then run your application
>     
>     If eclipse/any other IDE
>     
>     Close existing, right clicl eclipse,select run as admim and then run your application
> 
> Hope this helps


来源:https://stackoverflow.com/questions/12876743/creating-file-on-windows-7-causes-access-denied-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!