在Windows 7上创建文件会导致“拒绝访问”例外 [英] Creating file on Windows 7 causes "Access denied" exception

查看:396
本文介绍了在Windows 7上创建文件会导致“拒绝访问”例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序,用户可以使用以下代码创建文本文件并将其保存在计算机上的任何位置:

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.

但许多使用Windows 7的用户报告将文件保存到C:\ ,他们得到访问被拒绝错误。我发现这是因为他们需要管理员权限才能在Win7中以这样的路径保存文件。

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.

而不是向用户显示警告消息:你无法将文件保存在此路径,我可以以某种方式将文件保存在此路径中,例如是否有通过Java代码在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 ?

推荐答案

简答 - 否。

如果你需要保存到C盘,他们需要权限。如果此程序只需要创建文件,则可以使用用户临时文件夹。请参见System.getProperty()

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()

这篇关于在Windows 7上创建文件会导致“拒绝访问”例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆