title: How to read multiple files from ftp server in java?
body: <p>I have ftp server as follows</p>
  
  <pre><code>MainFolder 
   -Location1
     -download.txt
     -otherFiles...
   -Location2
     -download.txt
     -otherFiles...
   -Location3
     -download.txt
     -otherFiles...
   -LocationN
     -download.txt
     -otherFiles...
  </code></pre>
  
  <p>I want to read <code>download.txt</code> file from each Location folder and insert in db</p>
  
  <p>I have written below code but it only reads <code>download.txt</code> file of <code>Location1</code> folders, it does not read other <code>download.txt</code> files from rest of the Location folders. </p>
  
  <p>What's wrong with my code..?</p>
  
  <p>Below is my code</p>
  
  <pre><code>public class ReadFtpFile {
  
      static Logger logger = Logger.getLogger(ReadFtpFile.class);
      public static String MAIN_FOLDER_PATH = "MAIN_FOLDER";
      public static String fileNameToBeRead = "download";
  
  
       public static void main(String[] args) {
  
        String ftpUrl = "10.x.x.x";  
        String user = "username";
        String pass = "password";
  
        FTPFile[] listOfFolders =null;
        int location = 0;
        FTPFile[] listOfFiles = null;
  
         // get an ftpClient object  
         FTPClient ftpClient = new FTPClient();  
  
  
          // pass directory path on server to connect  
          ftpClient.connect(ftpUrl);  
  
          // pass username and password, returned true if authentication is successful  
          boolean login = ftpClient.login(user, pass); 
  
          FTPFileFilter filter = new FTPFileFilter() {
                 @Override
                 public boolean accept(FTPFile ftpFile) {
                    return (ftpFile.isFile() &amp;&amp; ftpFile.getName().contains(fileNameToBeRead));
                 }
          };
  
  
          if (login) {  
           logger.info("Connection established...");  
  
           FTPFile[] locationfolders = ftpClient.listDirectories(MAIN_FOLDER_PATH);  
  
           for (FTPFile folder : locationfolders) {  
  
            if(folder.isDirectory()){
                logger.info("Folder Found : "  + folder.getName());
                String subFolder = MAIN_FOLDER_PATH + "/" +folder.getName();
                FTPFile[] subfiles = ftpClient.listFiles(subFolder,filter); 
                for(FTPFile myfile : subfiles){
                    logger.info("File Name: "  + myfile.getName());
                    BufferedReader in = new BufferedReader(new InputStreamReader(ftpClient.retrieveFileStream(subFolder+ "/" +myfile.getName())));
                    logger.info("Reading file start.");
  
                    String inputLine;
                    while ((inputLine = in.readLine()) != null){
                     // logger.info(inputLine);
                    }
                    in.close();
                    logger.info("File Reading Completed");
                }
            }
  
           } 
  
           // logout the user, returned true if logout successfully  
           boolean logout = ftpClient.logout();  
           if (logout) {  
            logger.info("Connection close...");  
           }  
          } else {  
           logger.info("Connection fail...");  
          }     
       }
      }
  </code></pre>
  
  <p>OUTPUT :</p>
  
  <pre><code>Connection established...
  22-05-2017 10:54:49 INFO  ReadFtpFile:74 - Folder Found : Location1
  22-05-2017 10:54:49 INFO  ReadFtpFile:78 - File Name: download.txt
  22-05-2017 10:54:49 INFO  ReadFtpFile:80 - Reading file start.
  22-05-2017 10:54:49 INFO  ReadFtpFile:87 - File Reading Completed
  22-05-2017 10:54:49 INFO  ReadFtpFile:74 - Folder Found : Location2
  22-05-2017 10:54:49 INFO  ReadFtpFile:74 - Folder Found : Location3
  22-05-2017 10:54:49 INFO  ReadFtpFile:74 - Folder Found : LocationN
  </code></pre>