troubleshooting Question

FTP search to a directory using an array of search patterns

Avatar of kalyangkm
kalyangkmFlag for United States of America asked on
Java
8 Comments1 Solution271 ViewsLast Modified:
Hi Folks,

I have a rek to search a FTP directory for a list of filenames against an array of filepatterns. If there is match to the filepattern in the directory with a filename, that filename should be returned if otherwise it the program should return a blank or " ".

Example
if the input is a array of filepatterns as below
loook
lookup
loookup

and if the FTP directory contains
lookup.txt

then the output should be an array of Strings as follows
" "
lookup.txt
" "

So far I have the following program working for extracting the matched filenames in the directory based on filepatters, but it suppress the output if there is not match, instead I would need to return " " when there is no match for a particular pattern.

First I have created the following class

public class FTPMultipleFileLookup implements FTPFileFilter {

   	    
        FTPClient ftpClient = new FTPClient();
        FTPFile[] result;
        List<String> filePatterns;
        String pathname;
        FTPFileFilter filter;
        
  
        public FTPMultipleFileLookup(List<String> filePatterns) {
            this.filePatterns = filePatterns;
             }
   

       
          

	 @Override
	 public boolean accept(FTPFile ftpFile) {

	return (ftpFile.isFile() && filePatterns.contains(ftpFile.getName()));
		
	  }  

}

Open in new window




Then I used the above code in the Main program in which I perform the actual search but like I said the search only extracts the file pattern match records and suppress anyvalue that doesnt match, instead I need to return " ". Please suggest modification to the following program or any other approach that works.

public class MultipleFilelookup {
 
    public static void main(String[] args) {
    	 String server = "XXXXX";
         int port = XX;
         String user = "XXX";
         String pass = "XXXXXX";

         //final String file = "lookup.txt";
       
         //List<String> filePatterns = new ArrayList<String>();
         List<String> filePatterns = Arrays.asList("lookup.txt","loookup.txt");
         FTPClient ftpClient = new FTPClient();
  
      try{
             ftpClient.connect(server, port);
  
             ftpClient.login(user, pass);
  
             ftpClient.enterLocalPassiveMode();
  
             String dirToSearch = "XXXXXXX";
 
             FTPMultipleFileLookup filter = new FTPMultipleFileLookup(filePatterns) {
 
                @Override
                 public boolean accept(FTPFile ftpFile) {
 
               
                   boolean accept = false;
                	String fileName = ftpFile.getName();
                	for(String pattern : filePatterns){
                		if(fileName.contains(pattern)){
                			accept = true;
                			break;
                		} 	
                	}
                	return accept;

                }
            };
 
            FTPFile[] result = ftpClient.listFiles(dirToSearch, filter);
 
            if (result != null && result.length > 0) {
                System.out.println("SEARCH RESULT:");
                
                //InputStreamReader reader = new InputStreamReader(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();
                }
            }
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 8 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 8 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004