What is FileUtils.isRegularFile() in Java?

Overview

isRegularFile() is a staticthe methods in Java that can be called without creating an object of the class method of the FileUtils class that is used to check whether or not a given file is a regular file.

Regular files are neither directories nor special files. They are primarily used to store text or binary data.

How to import FileUtils

The definition of FileUtils can be found in the Apache Commons IO package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
</dependency>

For other versions of the commons-io package, refer to the Maven Repository.

You can import the FileUtils class as follows:


import org.apache.commons.io.FileUtils;

Syntax

public static boolean isRegularFile(final File file, final LinkOption... options)

Parameters

  • final File file: The file to check.
  • final LinkOption... options: Options that indicate how symbolic links are handled.

Return value

This method returns true if the file is a regular file. Otherwise, it returns false.

Code

main.java
1.txt
import org.apache.commons.io.FileUtils;
import java.io.File;
public class Main{
public static void main(String[] args) {
// Example 1
String filePath = "1.txt";
File file = new File(filePath);
System.out.println("The output of FileUtils.isRegularFile(1.txt) is - " + FileUtils.isRegularFile(file));
// Example 2
filePath = "2.txt";
file = new File(filePath);
System.out.println("The output of FileUtils.isRegularFile(2.txt) is - " + FileUtils.isRegularFile(file));
}
}

Explanation

Example 1

  • file: 1.txt

The method returns true because the file is available and is a regular file.

Example 2

  • file: 2.txt

The method returns false because the file is not available.

Output

The output of the code is as follows:

The output of FileUtils.isRegularFile(1.txt) is - true
The output of FileUtils.isRegularFile(2.txt) is - false

Free Resources

Attributions:
  1. undefined by undefined