我开发了一个应用程序,它使用 apache Fileutils 类方法将文件从源目录移动到目标目录,如下所示。
private void filemove(String FilePath2, String s2) {
String filetomove = FilePath2 + s2; //file to move its complete path
File f = new File(filetomove);
File d = new File(targetFilePath); // path of target directory
try {
FileUtils.copyFileToDirectory(f, d);
f.delete(); //from source dirrectory we are deleting the file if it succesfully move
//*********** code need to add to delete the zip files of target directory and only keeping the latest two zip files ************//
} catch (IOException e) {
String errorMessage = e.getMessage();
logger.error(errorMessage);
}
}
现在,当我将文件移动到目标目录时,因此在这种情况下,目标目录将包含某些 zip 文件,现在目标目录中的这些 zip 文件是由其他一些作业创建的,该作业运行一些创建 zip 文件的过程,但是任何我正在尝试的是,当我将文件移动到目标目录时,因此在保留目标目录之前,它应该检查目标目录并应该同时删除 zip 文件,它应该确保只有两个最新的 zip 文件不应该删除,所以最后目标目录应该有我正在移动的文件和最新的两个 zip 文件,请告知如何实现这一点。
所以请告知登录名,当它将文件移动到目标目录时,它应该删除目标目录的所有 zip 文件,只保留最近的两个 zip 文件
请各位大侠指教