isSymlink()
is a FileUtils
class that is used to check if the specified file is a symbolic link rather than an actual file.
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 isSymlink(final File file)
final File file
: This is the file to check.This method returns true
if the file is a symbolic link. 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.isSymlink(1.txt) is - " + FileUtils.isSymlink(file));// Example 2filePath = "2.txt";file = new File(filePath);System.out.println("The output of FileUtils.isSymlink(2.txt) is - " + FileUtils.isSymlink(file));// Example 3filePath = "/usr/local/bin/perror";file = new File(filePath);System.out.println("The output of FileUtils.isSymlink(/usr/local/bin/perror) is - " + FileUtils.isSymlink(file));}}
file - 1.txt
The method returns false
, as the file is available and is not a symbolic link.
file - 2.txt
The method returns false
, as the file is not available.
file - "/usr/local/bin/perror"
The method returns true
, as the file is available and is a symbolic link.
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