我正在尝试编写一个工具,它会将更新的文件复制到文件夹结构中。此文件夹结构分为“实例”,因此根文件夹显示以下内容:
在这些目录中,还有其他目录,当然还有文件。因为我想更新这些文件夹中的一些文件(更新的文件在我的工具可以访问的另一个文件夹中),我搜索整个结构。更新的文件名与Test1,Test2和Test3及其子文件夹中的旧文件名相同。
我查找了一个递归方法来浏览这些文件夹并进行搜索。当我的工具在同一个实例中找到第二个文件时,它必须抛出一个错误并结束for循环。
此外,在查找一个更新文件(patchfile)之后,临时列表(我检查是否存在重复项)将被清空。这一切都很好,就像在代码中一样。
但是期待我想用补丁文件夹中的补丁文件替换实例中的旧文件的步骤,我感到困惑,不知道如何工作。
我计划映射旧(扫描)文件的位置(地图中的值)和实际的补丁文件(键),然后处理地图中的元素。 但是我在哪里可以完美地放置put函数?除了递归的扫描方法之外,我的main-methode中的嵌套循环(迭代实例并迭代许多补丁文件)例如,让我困惑的地方放“map.put(...)”。
到目前为止,这是我的代码。另外,如果您有优化方面的建议,我会很高兴听到。
进一步提及:我的工具通过xml和JAXB获取实例的父文件夹,并将实例列表扫描到ArrayList(instances - > getInstances())中迭代。
public class Main {
public static void main(String[] args) throws SAXException, JAXBException {
XMLHandler xmlhandler = new XMLHandler();
InstanceManager manager = new InstanceManager();
PatchFileManager patchManager = new PatchFileManager(xmlhandler.getUpdateFolder());
patchManager.scanDirectoryForPatches();
ArrayList<PatchFile> patchFilesname = patchManager.getPatchFiles();
manager.setPathnameToScan(xmlhandler.getTargetFolder());
manager.scanDirectoryForInstances(manager.getPathnameToScan());
PatchManager ptchmngr = new PatchManager();
outerloop:
for (PatchFile f : patchFilesname) {
for (Instance e : manager.getInstances()) {
ptchmngr.scanInstallationForPatchFile(e.getPathname(), f);
if(ptchmngr.getPatchSearchTempChecker().size()>1){
System.out.println("loop stopped");
break outerloop;
}
}
}
....
}
}
实际的类,其中扫描文件夹发生(排除getter和setter)
public class PatchManager {
private InstanceManager theInstanceManager;
private ArrayList<BackUp> backUpList = new ArrayList<BackUp>();
private ASPXManager theAspxHandler;
private ArrayList<PatchFile> patchFileList = new ArrayList<PatchFile>();
private LogManager theLogManager;
private ArrayList<String> patchSearchTempChecker = new ArrayList<String>();
private HashMap map = new HashMap();
public void copyPatch(PatchFile patchfile) {
}
public void searchFiles(PatchFile patchFile) {
}
public void createBackUp(PatchFile patchfile) {
}
public void scanInstallationForPatchFile(String searchstart, PatchFile patchfile) {
File filetoSearch = new File(searchstart);
try {
if (filetoSearch.isDirectory()) {
System.out.println("Searching directory..." + filetoSearch.getAbsolutePath());
if (filetoSearch.canRead()) {
for (File temp : filetoSearch.listFiles()) {
if (patchSearchTempChecker.size() > 1) {
throw new DoubleDefinitionException();
}
if (temp.isDirectory() && !(temp.getName().equals("Images"))) {
scanInstallationForPatchFile(temp.getAbsolutePath(), patchfile);
} else {
if (patchfile.getFilename().toLowerCase().equals(temp.getName().toLowerCase())) {
patchSearchTempChecker.add(temp.getAbsolutePath().toString());
System.out.println(patchSearchTempChecker.size());
}
}
}
}
} else {
System.out.println("Permission denied");
}
} catch (
DoubleDefinitionException ex)
{
System.out.println("File does exist multiple times within instance, proceed manually");
}
}
//Getters and Setters
//...
//
}