如何只读取12个文件因为我只读取了ftp中的所有文件 服务器,文件每4分钟添加一个新文件。
感谢您的帮助,我是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 :(得分:1)
for (int i = 0; i < files.length && i < 12; ++i) {
FTPFile file = files[i];
...
}