我有以下代码,我需要捕获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");
}
}
}
如果sample.txt是只读的,我输出为“找不到文件”而不是“访问已拒绝”.我想了解这是什么原因?另外,上述捕获AccessDeniedException的结构是否正确?