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:FTPFileFilter filter = new FTPFileFilter() {
@Override
public boolean accept(FTPFile ftpFile) {
return ftpFile.isDirectory();
}
}; FTPFileFilter filter = new FTPFileFilter() {
@Override
public boolean accept(FTPFile ftpFile) {
return (ftpFile.isFile() && ftpFile.getName().endsWith(".html"));
}
}; FTPFileFilter filter = new FTPFileFilter() {
@Override
public boolean accept(FTPFile ftpFile) {
return (ftpFile.isFile() && ftpFile.getName().contains("Java"));
}
}; 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.md5NOTES: The listFiles() method does not search in sub directories, it only searches for files and directories underneath the given directory.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.