Java : AccessDeniedException 示例

Java 中的 AccessDeniedException (Java SE 23 & JDK 23) 及其示例。
您将找到大多数 AccessDeniedException 方法的代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

当文件系统操作被拒绝时抛出检查异常,通常是由于文件权限或其他访问检查。 (机器翻译)

Class diagram

final var dir = Path.of("R:", "java-work", "dir");
System.out.println(dir); // R:\java-work\dir

Files.createDirectory(dir);
System.out.println(Files.isDirectory(dir)); // true

try {
    Files.writeString(dir, "abc");
} catch (AccessDeniedException e) {
    System.out.println("AccessDeniedException!");
}

// Result
// ↓
//AccessDeniedException!

Constructors

AccessDeniedException (String file)

构造此类的一个实例。 (机器翻译)

final var e = new AccessDeniedException("aaa.txt");
System.out.println(e); // java.nio.file.AccessDeniedException: aaa.txt
System.out.println(e.getFile()); // aaa.txt

AccessDeniedException (String file, String other, String reason)

构造此类的一个实例。 (机器翻译)

final var e = new AccessDeniedException("aaa.txt", "bbb.txt", "Reason!");
System.out.println(e); // java.nio.file.AccessDeniedException: aaa.txt -> bbb.txt: Reason!
System.out.println(e.getFile()); // aaa.txt
System.out.println(e.getOtherFile()); // bbb.txt
System.out.println(e.getReason()); // Reason!

Methods declared in FileSystemException

getFile, getMessage, getOtherFile, getReason

请参阅下面的链接。

Methods declared in Throwable

addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString

请参阅下面的链接。


相关文章

To top of page