Java java.nio.file.Files.isRegularFile() Method
java.nio.file.Files class is an important utility class in the Java NIO package for operating on files and directories. Among them, isRegularFile() method is a very practical method for checking whether the specified path corresponds to a "regular file". This article will detail the method's usage, parameter meanings, and practical application scenarios.
Method Definition
public
static
boolean
isRegularFile
(
Path path, LinkOption...
options
)
throws
IOException
Parameter Description
path: The file path to check (Pathobject)options(optional): An array of link options used to specify how to handle symbolic links. A commonly used option isLinkOption.NOFOLLOW_LINKS, which means do not follow symbolic links
Return Value
- If the file corresponding to the path exists and is a regular file, returns
true - Other cases return
false(including the file does not exist, is a directory, is a symbolic link, etc.)
exception
- If an I/O error occurs, throws
IOException
What is a regular file?
In a file system, a "regular file" refers to:
- A file that is not a directory
- A file that is not a symbolic link
- Not a special device file (such as in Unix systems
/devfiles under )
Simply put, regular files are the ordinary files we usually create, such as text files, images, and executable programs.
Usage examples
Basic Usage
Example
import
java.nio.file.*
;
public
class
IsRegularFileExample
{
public
static
void
main
(
String
[
]
args
)
{
Path filePath
=
Paths.
get
(
"example.txt"
)
;
try
{
boolean
isRegular
=
Files.
isRegularFile
(
filePath
)
;
System
.
out
.
println
(
"Is regular file: "
isRegular
)
;
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
}
}
Handling symbolic links
Example
import
java.nio.file.*
;
public
class
SymbolicLinkExample
{
public
static
void
main
(
String
[
]
args
)
{
Path symLinkPath
=
Paths.
get
(
"symlink_to_file"
)
;
try
{
// Check without following symbolic links
boolean
isRegular
=
Files.
isRegularFile
(
symLinkPath, LinkOption.
NOFOLLOW_LINKS
)
;
System
.
out
.
println
(
"Is regular file (no follow): "
isRegular
)
;
// Check following symbolic links by default
isRegular
=
Files.
isRegularFile
(
symLinkPath
)
;
System
.
out
.
println
(
"Is regular file (follow): "
isRegular
)
;
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
}
}
Comparison with Other Methods
The difference between Files.exists() and
exists()only checks whether the path exists, regardless of file typeisRegularFile()not only checks existence but also confirms it is a regular file
The difference between Files.isDirectory() relationship
These two methods are complementary:
isRegularFile()checks whether it is a regular fileisDirectory()Check whether it is a directory
Practical Application Scenarios
- File upload validation: Verify whether the user-selected file is a regular file before uploading
- File processing: Confirm the file type before processing the file
- File system scanning: Distinguish files from subdirectories when traversing a directory
Example
// Practical example: count the number of regular files in a directory
public
long
countRegularFiles
(
Path dir
)
throws
IOException
{
return
Files.
list
(
dir
)
.
filter
(
path
->
Files.
isRegularFile
(
path
)
)
.
count
(
)
;
}
Notes
- Performance considerations: The method needs to access the file system; frequent calls may affect performance
- Race condition: The check result may become invalid immediately (the file may be deleted or modified after the check)
- Symbolic link handling: Clarify whether symbolic links need to be followed
- Exception handling: Always handle possible
IOException
Summary
Files.isRegularFile() is a simple but practical method in Java NIO file operations that helps us accurately determine whether a path points to a regular file. Using this method correctly can avoid many common errors in file operations and make our code more robust and reliable.
In practical development, it is recommended to combine with other Files class methods (such as exists(), isDirectory() , etc.) for comprehensive handling of various file system operation scenarios.
Java java.nio.file.Files