Java FTP example - Search for files and directories
- Details
- Written by Nam Ha Minh
- Last Updated on 20 July 2019   |   Print Email
In the article Java FTP list files and directories example, we described how to query all files and sub directories in a given directory. However, in some particular cases, we don’t need to list everything inside a directory. Instead, we just want to search for only the files and directories which meet some certain criteria, e.g. only files; only directories; only files that have .html extension; etc. To do so, we can use the following method of the FTPClientclass:
FTPFile[] listFiles(String pathname, FTPFileFilter filter)
In this method, the noticeable parameter is FTPFileFilter which is used to filter out the listing result. This interface declares a single method:
boolean accept(FTPFile file)
So to provide our own filter implementation, we have to create a class that implements the FTPFileFilter interface and its accept() method. For example:
FTPFileFilter filter = new FTPFileFilter() { @Override public boolean accept(FTPFile ftpFile) { return ftpFile.isFile(); } };
This filter implementation class will return only files (filter out directories) in the listing result. Then we can call the listFiles() method as follows:
FTPFile[] result = ftpClient.listFiles("Upload", filter);
Often, the more compact syntax is used:
FTPFile[] result = ftpClient.listFiles("Upload", new FTPFileFilter() { @Override public boolean accept(FTPFile ftpFile) { return ftpFile.isFile(); } });
Here are some other examples of implementing the FTPFileFilter interface:
- Search for only directories:
FTPFileFilter filter = new FTPFileFilter() { @Override public boolean accept(FTPFile ftpFile) { return ftpFile.isDirectory(); } };
- Search for only .html files:
FTPFileFilter filter = new FTPFileFilter() { @Override public boolean accept(FTPFile ftpFile) { return (ftpFile.isFile() && ftpFile.getName().endsWith(".html")); } };
- Search for only files whose name contain the word “Java”:
FTPFileFilter filter = new FTPFileFilter() { @Override public boolean accept(FTPFile ftpFile) { return (ftpFile.isFile() && ftpFile.getName().contains("Java")); } };
The following is a complete example program that connects to a FTP server then searches for only files containing the word “Java” in their names:
package net.codejava.ftp; import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPFileFilter; /** * This program demonstrates how to search for files and directories on a FTP * server using the Apache Commons Net API. * @author www.codejava.net * */ public class FTPSearchDemo { public static void main(String[] args) { String server = "www.myserver.com"; int port = 21; String user = "username"; String pass = "password"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(user, pass); ftpClient.enterLocalPassiveMode(); String dirToSearch = "HNS/Website/Articles"; FTPFileFilter filter = new FTPFileFilter() { @Override public boolean accept(FTPFile ftpFile) { return (ftpFile.isFile() && ftpFile.getName().contains("Java")); } }; FTPFile[] result = ftpClient.listFiles(dirToSearch, filter); if (result != null && result.length > 0) { System.out.println("SEARCH RESULT:"); for (FTPFile aFile : result) { System.out.println(aFile.getName()); } } ftpClient.logout(); ftpClient.disconnect(); } catch (IOException ex) { ex.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException ex) { ex.printStackTrace(); } } } } }
Sample output of the above program:
SEARCH RESULT: Code_JavaCore_2013-07-28.rar Code_JavaCore_2013-07-28.rar.md5 Code_JavaEE_2013-07-13.rar Code_JavaEE_2013-07-13.rar.md5 Code_JavaSE_2013-07-13.rar Code_JavaSE_2013-07-13.rar.md5 Code_JavaSE_2013-07-28.rar Code_JavaSE_2013-07-28.rar.md5
NOTES: The listFiles() method does not search in sub directories, it only searches for files and directories underneath the given directory.
Related Java FTP Tutorials:
- Java FTP list files and directories example
- List files and directories recursively on a FTP server
- Creating nested directory structure on a FTP server
- Java FTP example - Calculate total sub directories, files and size of a directory
- Determine if a directory or file exists on FTP server
- Remove an empty directory on a FTP server
- Rename file or directory on FTP server
Other Java FTP Tutorials:
- Connect and login to a FTP server
- Java FTP create directory example
- Java FTP file download tutorial and example
- Java FTP file upload tutorial and example
- Java FTP delete file example
- How to upload a directory to a FTP server
- How to download a complete folder from a FTP server
Comments