我有一個文件夾與此結構的Java複製文件夾排除一些內部文件
mainFolder
--Sub1
--File .scl
--File .awl
--Other files
--Sub2
--Files
--Sub3
--Sub4
我想將它複製到另一個位置,但我想避免Sub3中和(根據來自情況)從Sub1的
這裏是一些文件是從我做了什麼至今的摘錄:
FileUtils.copyDirectory(srcDir, dstDir, new FileFilter() {
public boolean accept(File pathname) {
// We don't want 'Sub3' folder to be imported
// + look at the settings to decide if some format needs to be
// excluded
String[] ignoreList= new String[]{
!Settings.getSiemensOptionAWL() ? ".awl":"uselessStringWilNeverBeFound",
!Settings.getSiemensOptionSCL() ? ".scl":"uselessStringWilNeverBeFound",
"Sub3"
};
return !(ignoreFile(pathname, ignoreList) && pathname
.isDirectory());
}
}, true);
public static boolean ignoreFile(File file, String[] ignoreList) {
for (final String ignoreStr : ignoreList)
if (file.getAbsolutePath().contains(ignoreStr))
return true;
return false;
}
顯然它接縫工作。但我認爲是一個非常醜陋的解決方案.... 有沒有人知道更好的方法?
P.S:當然Settings.getSiemensOptionAWL的 ()僅僅是布爾函數taht返回我的決定
將整個目錄複製到新位置可能很容易,然後刪除該文件(來自該副本)不應該在那裏。顯然,如果動機是與安全相關的而不是基本功能,則這不合適。 –
我明白你是什麼意思......但是想象一下你不會有什麼用戶有複製數據的風險,你不知道用戶擁有什麼。(並非如此,但我想要一個乾淨的解決方案:) :) – Stefano
甚至...顯然有時它的工作原理,但有時它不......可能只是一個更好的方式來相同的想法! – Stefano