Java : FileSystemException with Examples

FileSystemException (Java SE 21 & JDK 21) with Examples.
You will find code examples on most FileSystemException methods.


Summary

Thrown when a file system operation fails on one or two files. This class is the general class for file system exceptions.

Class diagram

final var file = Path.of("R:", "java-work", "aaa.txt");
System.out.println(file); // R:\java-work\aaa.txt
System.out.println(Files.notExists(file)); // true

try {
    Files.delete(file);
} catch (FileSystemException e) {
    System.out.println(e);
}

// Result
// ↓
//java.nio.file.NoSuchFileException: R:\java-work\aaa.txt
final var file = Path.of("R:", "java-work", "aaa.txt");
System.out.println(file); // R:\java-work\aaa.txt

Files.createFile(file);
System.out.println(Files.exists(file)); // true

try {
    Files.createFile(file);
} catch (FileSystemException e) {
    System.out.println(e);
}

// Result
// ↓
//java.nio.file.FileAlreadyExistsException: R:\java-work\aaa.txt

Constructors

FileSystemException (String file)

Constructs an instance of this class.

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

FileSystemException (String file, String other, String reason)

Constructs an instance of this class.

final var e = new FileSystemException("aaa.txt", "bbb.txt", "Reason!");
System.out.println(e); // java.nio.file.FileSystemException: 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

String getFile ()

Returns the file used to create this exception.

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

String getMessage ()

Returns the detail message string.

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

String getOtherFile ()

Returns the other file used to create this exception.

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

String getReason ()

Returns the string explaining why the file system operation failed.

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

Methods declared in Throwable

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

Please see the link below.


Related posts

To top of page