2015-06-23 118 views
-1

如何只讀取12個文件導致我只讀取ftp 服務器中的所有文件,每4分鐘文件添加一個新文件。如何在ftp服務器上只讀取十二個文件?

感謝您的幫助,我是初學者在API編程:)

if(files.length>12){ //amount list of files 
        for (FTPFile file : files) { 
         try { 
          System.out.println("      " 
            + file.getName()); 
          fileName = "/AGIN/" + dir + "/" + file.getName(); 
          iStream = ftpClient.retrieveFileStream(fileName); 
          br = new BufferedReader(new InputStreamReader(
            iStream)); 
          sb = new StringBuffer(); 
          while ((line = br.readLine()) != null) { 
           sb.append(line); 
          } 
          String xmlStr = "<betRecordList>" + sb.toString() 
            + "</betRecordList>"; 
          jaxbContext = JAXBContext 
            .newInstance(BetRecordList.class); 
          jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
          ByteArrayInputStream bis = new ByteArrayInputStream(
            xmlStr.getBytes()); 
          StreamSource ss = new StreamSource(bis); 
          returnlist = jaxbUnmarshaller.unmarshal(ss, 
            returnClazz); 
          ret = (BetRecordList) returnlist.getValue(); 
          System.out.println("" + ret.getRecord()); // the xml files read from the ftp server 

         } catch (Exception e) { 
          e.printStackTrace(); 
         } finally { 
          try { 
           if (iStream != null) { 
            iStream.close(); 
            iStream = null; 
           } 
           if (bInf != null) { 
            bInf.close(); 
            bInf = null; 
           } 

           boolean isComplete = ftpClient 
             .completePendingCommand(); 
           System.out.println("isComplete::" + isComplete); 
          } catch (Exception e) { 
           e.printStackTrace(); 
          } 
         } 

        } 
       } 

       boolean logout = ftpClient.logout(); 
       if (logout) { 
        System.out.println("Connection close"); 
       } 
      } else { 
       System.out.println("Connection fail"); 
      } 

     } catch (SocketException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (iStream != null) { 
        iStream.close(); 
       } 
       if (bInf != null) { 
        bInf.close(); 
       } 
       ftpClient.disconnect(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return ret.getRecord(); 
    } 
+0

可能重複[如何在Java中使用FTP客戶端獲得FTP服務器的十二個最新的文件(HTTP:/ /stackoverflow.com/questions/30883025/how-to-get-the-twelve-latest-file-in-ftp-server-using-ftp-client-in-java) –

回答

1
for (int i = 0; i < files.length && i < 12; ++i) { 
    FTPFile file = files[i]; 
    ... 
} 
+0

非常感謝你蘭斯Java :) –

相關問題