What is FileUtils.isSymlink() in Java?

Overview

isSymlink() 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 if the specified file is a symbolic link rather than an actual file.

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 isSymlink(final File file)

Parameters

  • final File file: This is the file to check.

Return value

This method returns true if the file is a symbolic link. 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.isSymlink(1.txt) is - " + FileUtils.isSymlink(file));
// Example 2
filePath = "2.txt";
file = new File(filePath);
System.out.println("The output of FileUtils.isSymlink(2.txt) is - " + FileUtils.isSymlink(file));
// Example 3
filePath = "/usr/local/bin/perror";
file = new File(filePath);
System.out.println("The output of FileUtils.isSymlink(/usr/local/bin/perror) is - " + FileUtils.isSymlink(file));
}
}

Explanation

Example 1

  • file - 1.txt

The method returns false, as the file is available and is not a symbolic link.

Example 2

  • file - 2.txt

The method returns false, as the file is not available.

Example 3

  • file - "/usr/local/bin/perror"

The method returns true, as the file is available and is a symbolic link.

Output

The output of the code will be as follows:


The output of FileUtils.isSymlink(1.txt) is - false
The output of FileUtils.isSymlink(2.txt) is - false
The output of FileUtils.isSymlink(/usr/local/bin/perror) is - true

Free Resources

Attributions:
  1. undefined by undefined