Java java.nio.file.Files.isDirectory() Method
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
Path path: The path object to checkLinkOption... options(optional): An optional array of parameters specifying how to handle symbolic links
Return Value
true: If the path exists and is a directoryfalse: If the path does not exist, is not a directory, or cannot be determined for some reason
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
- Convenience: More modern than traditional
File.isDirectory()method, supports more features - Safety: Automatically handles security checks to avoid vulnerabilities
- Symbolic link handling: Can flexibly control how symbolic links are handled
- Exception handling: Does not throw an exception but returns
falseto 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
- Permission issues: If the program does not have permission to read the target path, the method returns
false - Concurrent Modification: The check result may become outdated immediately because the file system may be modified by other programs
- Performance considerations: Frequent calls to this method may affect performance; consider caching the result
- Path resolution: Relative paths are resolved relative to the current working directory
Best Practices
- Always check the return value; do not assume the path is definitely a directory
- For critical operations, consider combining with
Files.exists()for use together - When processing user-provided paths, use
NOFOLLOW_LINKSoption to increase security - 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 java.nio.file.Files