isRegularFile()
is a 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.
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;
public static boolean isRegularFile(final File file, final LinkOption... options)
final File file
: The file to check.final LinkOption... options
: Options that indicate how symbolic links are handled.This method returns true if the file is a regular file. Otherwise, it returns false.
import org.apache.commons.io.FileUtils;import java.io.File;public class Main{public static void main(String[] args) {// Example 1String filePath = "1.txt";File file = new File(filePath);System.out.println("The output of FileUtils.isRegularFile(1.txt) is - " + FileUtils.isRegularFile(file));// Example 2filePath = "2.txt";file = new File(filePath);System.out.println("The output of FileUtils.isRegularFile(2.txt) is - " + FileUtils.isRegularFile(file));}}
file: 1.txt
The method returns true because the file is available and is a regular file.
file: 2.txt
The method returns false because the file is not available.
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