Search code examples
javaapache-commons-net

Java FTPFile List with condition


I'm retreiving the list of files from an FTP site using the code below using the commons-net:

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

private void ExtractFiles(FTPClient ftpClient) {

    ftpPath = "/home/user1/output";
    FTPFile[] ftpFiles = null;

    try {
        ftpFiles = ftpClient.listFiles(ftpPath);

    } catch (IOException e1) {
        e1.printStackTrace();
    }

    if (ftpFiles.length == 0) {
        return;
    }
}

My concern is the folder might have hundreds or in the future thousands of files that might cause the process to be slower. I want to add a condition to only extract the files (*.csv). Is it possible to add a condition in the line:

ftpFiles = ftpClient.listFiles(ftpPath, if (*.csv)) ;

Something like that? thank you


Solution

  • Yes, and there is another listFiles that takes a filter. Here is the document.