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

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

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

Java File Java java.nio.file.Files

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

Return Value

exception


What is a regular file?

In a file system, a "regular file" refers to:

  1. A file that is not a directory
  2. A file that is not a symbolic link
  3. Not a special device file (such as in Unix systems /dev files 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

The difference between Files.isDirectory() relationship

These two methods are complementary:


Practical Application Scenarios

  1. File upload validation: Verify whether the user-selected file is a regular file before uploading
  2. File processing: Confirm the file type before processing the file
  3. 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

  1. Performance considerations: The method needs to access the file system; frequent calls may affect performance
  2. Race condition: The check result may become invalid immediately (the file may be deleted or modified after the check)
  3. Symbolic link handling: Clarify whether symbolic links need to be followed
  4. 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 File Java java.nio.file.Files