Java : DirectoryNotEmptyException 示例
Java 中的 DirectoryNotEmptyException (Java SE 22 & JDK 22) 及其示例。
您将找到大多数 DirectoryNotEmptyException 方法的代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
final var dir = Path.of("R:", "java-work", "dir");
System.out.println(dir); // R:\java-work\dir
final var file = dir.resolve(Path.of("aaa.txt"));
System.out.println(file); // R:\java-work\dir\aaa.txt
Files.createDirectories(dir);
Files.createFile(file);
// --- PowerShell ---
//PS R:\java-work> tree /F
//...
//R:.
//└─dir
// aaa.txt
// --- NG ---
try {
System.out.println("-- delete --");
Files.delete(dir);
} catch (DirectoryNotEmptyException e) {
System.out.println(e);
}
// Result
// ↓
//-- delete --
//java.nio.file.DirectoryNotEmptyException: R:\java-work\dir
// --- OK ---
Files.delete(file);
Files.delete(dir);
System.out.println(Files.notExists(dir)); // true
final var src = Path.of("R:", "java-work", "src-dir");
final var dst = Path.of("R:", "java-work", "dst-dir");
System.out.println(src); // R:\java-work\src-dir
System.out.println(dst); // R:\java-work\dst-dir
Files.createDirectories(src);
Files.createDirectories(dst);
Files.createFile(dst.resolve(Path.of("aaa.txt")));
// --- PowerShell ---
//PS R:\java-work> tree /F
//...
//R:.
//├─dst-dir
//│ aaa.txt
//│
//└─src-dir
try {
System.out.println("-- move --");
Files.move(src, dst, StandardCopyOption.REPLACE_EXISTING);
} catch (DirectoryNotEmptyException e) {
System.out.println(e);
}
// Result
// ↓
//-- move --
//java.nio.file.DirectoryNotEmptyException: R:\java-work\dst-dir
Constructors
DirectoryNotEmptyException (String dir)
final var e = new DirectoryNotEmptyException("Dir!");
System.out.println(e); // java.nio.file.DirectoryNotEmptyException: Dir!
System.out.println(e.getFile()); // Dir!
Methods declared in FileSystemException
Methods declared in Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
请参阅下面的链接。