Java : AccessDeniedException with Examples
AccessDeniedException (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the AccessDeniedException methods.
Summary
Checked exception thrown when a file system operation is denied, typically due to a file permission or other access check.
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)
Constructs an instance of this class.
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)
Constructs an instance of this class.
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
Please see the link below.
Methods declared in Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Please see the link below.