Creating file on Windows 7 causes quot;Access deniedquot; exception(在 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:" 时,他们会收到Access is denied"错误.我发现这是因为他们需要管理员权限才能在 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 上创建文件会导致“访问被拒绝"例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Windows 7 上创建文件会导致“访问被拒绝"例外
基础教程推荐
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
