Course contentsShow
← Java
Lesson 2069 of 2,87041. NIO.2 and the Path APIFree lesson

Files.isSymbolicLink() and Files.isHidden()

Identify symbolic links and check if files are marked as hidden on the operating system.

Files.isSymbolicLink() and Files.isHidden()

What you'll learn: How to detect symbolic links and check if files are marked as hidden by the operating system.

Understanding These File Attributes

Just as files can be readable or writable, they also have other special properties. A file might be a symbolic link (a pointer to another file, like a shortcut) or marked as hidden by the operating system. The Files class provides methods to check both attributes.

Checking for Symbolic Links

Files.isSymbolicLink(Path path) returns true if the path points to a symbolic link, false otherwise.

Path link = Paths.get("/tmp/shortcut");
if (Files.isSymbolicLink(link)) {
    // This is a symbolic link to another file/directory
}

Important: This checks if the path itself is a symbolic link, not whether it points through links. It doesn't follow the link—it examines the path directly.

Checking for Hidden Files

Files.isHidden(Path path) returns true if the operating system marks the file as hidden. This is platform-dependent:

  • Windows: Files with the "hidden" attribute set
  • Unix/Linux: Files whose names start with a dot (.)
Path file = Paths.get("/home/user/.bashrc");
if (Files.isHidden(file)) {
    // This file is hidden according to the OS
}

Note: Unlike isSymbolicLink(), this method can throw IOException if it cannot determine the hidden status.

Practical Example

Path path = Paths.get("myfile.txt");

if (Files.isSymbolicLink(path)) {
    // Handle symbolic links specially
}

try {
    if (Files.isHidden(path)) {
        // Handle hidden files
    }
} catch (IOException e) {
    // Could not determine hidden status
}

Key Takeaway: Use Files.isSymbolicLink() to identify symbolic links (without following them) and Files.isHidden() to check OS-specific hidden file attributes—both help you handle files according to their special properties.