Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java java.nio.file.Files isDirectory() Method

Java java.nio.file.Files.isDirectory() Method

Java File Java java.nio.file.Files

java.nio.file.Files.isDirectory() is a very practical method in the Java NIO (New I/O) package for checking whether the specified path points to a directory. This article will detail the method's usage, parameters, return value, and practical application scenarios.

Method Definition

public

static

boolean

isDirectory

(

Path path, LinkOption...

options

)

Parameter Description

  1. Path path: The path object to check
  2. LinkOption... options(optional): An optional array of parameters specifying how to handle symbolic links

Return Value


Basic Usage

Check a normal directory

Example

import

java.nio.file.*

;

public

class

DirectoryCheck

{

   

public

static

void

main

(

String

[

]

args

)

{

        Path path

=

Paths.

get

(

"/path/to/directory"

)

;

       

       

if

(

Files.

isDirectory

(

path

)

)

{

           

System

.

out

.

println

(

"This is a directory"

)

;

       

}

else

{

           

System

.

out

.

println

(

"This is not a directory or the directory does not exist"

)

;

       

}

   

}

}

Handling symbolic links

By default,isDirectory() follows symbolic links. If you want to not follow symbolic links, you can use LinkOption.NOFOLLOW_LINKS:

Example

boolean

isDir

=

Files.

isDirectory

(

path, LinkOption.

NOFOLLOW_LINKS

)

;


Method Characteristics

  1. Convenience: More modern than traditional File.isDirectory() method, supports more features
  2. Safety: Automatically handles security checks to avoid vulnerabilities
  3. Symbolic link handling: Can flexibly control how symbolic links are handled
  4. Exception handling: Does not throw an exception but returns false to indicate check failure

Comparison with Other Methods

The difference between File.isDirectory() Comparison

Feature Files.isDirectory() File.isDirectory()
Exception handling Returns boolean Returns boolean
Symbolic link handling Configurable Automatically follows
Performance Higher Lower
Functionality Richer Basic Functionality

The difference between Files.exists() Comparison

Files.exists() only checks whether the path exists, while isDirectory() specifically checks whether the path exists and is a directory.


Practical Application Examples

Example 1: Traverse a directory

Example

import

java.nio.file.*

;

import

java.io.IOException

;

public

class

DirectoryWalker

{

   

public

static

void

main

(

String

[

]

args

)

throws

IOException

{

        Path startPath

=

Paths.

get

(

"/path/to/start"

)

;

       

        Files.

walk

(

startPath

)

            .

filter

(

Files

::

isDirectory

)

            .

forEach

(

System

.

out

::

println

)

;

   

}

}

Example 2: Security check

Example

public

boolean

isSafeDirectory

(

Path path

)

{

   

// Check whether it is a directory and not a symbolic link

   

return

Files.

isDirectory

(

path, LinkOption.

NOFOLLOW_LINKS

)

;

}


Notes

  1. Permission issues: If the program does not have permission to read the target path, the method returns false
  2. Concurrent Modification: The check result may become outdated immediately because the file system may be modified by other programs
  3. Performance considerations: Frequent calls to this method may affect performance; consider caching the result
  4. Path resolution: Relative paths are resolved relative to the current working directory

Best Practices

  1. Always check the return value; do not assume the path is definitely a directory
  2. For critical operations, consider combining with Files.exists() for use together
  3. When processing user-provided paths, use NOFOLLOW_LINKS option to increase security
  4. Consider using a try-catch block to handle potential SecurityException

Example

try

{

   

if

(

Files.

isDirectory

(

path

)

)

{

       

// Safe operation

   

}

}

catch

(

SecurityException

e

)

{

   

System

.

err

.

println

(

"No permission to access path: "

path

)

;

}


Summary

Files.isDirectory() is a simple but powerful method in Java NIO for checking whether a path is a directory. Compared with traditional File class methods, it is more flexible and safer, and is especially suitable for use in modern Java applications. By using this method properly, you can write more robust and safer file system operation code.

Java File Java java.nio.file.Files