Java - List all files and ignore symbolic links

By xngo on February 21, 2019

import java.io.File;
import java.nio.file.Files; // Require java version 1.7
import java.util.Collection;
 
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
 
/**
 * List all files and ignore symbolic links.
 * Provide your own implementation of *FileFilter.
 *
 */
public class CustomFileFilter
{
 
  public static void main(String[] args)
  {
    // Create a new file filter
    //  and override its accept-method to discard symbolic link.
    TrueFileFilter trueFileFilterNoSymbolicLink = new TrueFileFilter()
      {
          @Override
          public boolean accept(File file)
          {// Return false if it is symbolic link.
            if(Files.isSymbolicLink(file.toPath()))
              return false;
            else
              return true;
          }
 
      };              
 
    File file = new File("/some-path-containing-symbolic-links/");
    Collection<File> filesList = FileUtils.listFiles(file, 
                                                    trueFileFilterNoSymbolicLink, 
                                                    trueFileFilterNoSymbolicLink);
 
    for(File f: filesList)
    {
      System.out.println(f.getAbsolutePath());
    }
 
  }
 
}

Github

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.