Java中的AccessDeniedException异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22403058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AccessDeniedException exception in java
提问by Akg
I have the following code where I need to catch AccessDeniedException
exception
我有以下代码需要捕获AccessDeniedException
异常
import java.io.PrintWriter;
import java.io.IOException;
import java.nio.file.AccessDeniedException;
class MyFileClass {
public void write()
throws IOException
{
PrintWriter out = new PrintWriter("sample.txt");
out.printf("%8.2f\n", 3.4);
out.close();
}
}
public class MyClass {
public static void main(String[] args)
throws Exception
{
try {
MyFileClass mf = new MyFileClass();
mf.write();
} catch (AccessDeniedException e) {
print("Access denided");
}
catch (FileNotFoundException e) {
print("File not found");
}
}
}
In case sample.txtis read only, I get output as "file not found" rather "Access denided". I would like to understand what is the reason for this? Also, is the above structure for catching AccessDeniedException
correct?
如果sample.txt是只读的,我得到的输出是“找不到文件”而不是“拒绝访问”。我想了解这是什么原因?另外,上面的捕捉结构是否AccessDeniedException
正确?
采纳答案by fge
AccessDeniedException
is only thrown by the new file API; the old file API (which you use with this PrintWriter
constructor) only knows how to throw FileNotFoundException
even if the real filesystem-level problem is not "the file does not exist".
AccessDeniedException
仅由新文件 API 抛出;即使真正的文件系统级问题不是“文件不存在” ,旧的文件 API(您与此PrintWriter
构造函数一起使用)也只知道如何抛出FileNotFoundException
。
You have to use the new API to open an output stream to the destination file; thenyou can have meaningful exceptions:
你必须使用新的 API 打开一个输出流到目标文件;那么你可以有有意义的例外:
// _will_ throw AccessDeniedException on access problems
final OutputStream out = Files.newOutputStream(Paths.get(filename));
final PrintWriter writer = new PrintWriter(out);
More generally, the new file API defines FileSystemException
(inheriting IOException
), which all new, meaningful exceptions defined by the new API inherit.
更一般地说,新文件 API 定义FileSystemException
(继承IOException
),新 API 定义的所有新的、有意义的异常都会继承。
This means among other things that you can clearly separate, in catch clauses, what is caused by filesystem-level errors and "real" I/O errors, which you can't do with the old API:
这意味着除其他事项外,您可以在 catch 子句中明确区分由文件系统级错误和“真实”I/O 错误引起的原因,而旧 API 无法做到这一点:
try {
// some new file API operation
} catch (FileSystemException e) {
// deal with fs error
} catch (IOException e) {
// deal with I/O error
}
回答by AJ.
There is NOsuch AccessDeniedException
in PrintWriter
.
中没有这样AccessDeniedException
的PrintWriter
。
SecurityException
is the exception thrown by PrintWriter
SecurityException
是抛出的异常 PrintWriter
If a security manager is present and checkWrite(file.getPath()) denies write access to the file
如果存在安全管理器并且 checkWrite(file.getPath()) 拒绝对文件的写访问