英文:
How to check if the folder exists in subfolder of same level in java?
问题
I have folder structure as below and I need to check and copy files from AB1 folder to CD1 folder.
根文件夹
|___AB ------------------------>DD
|__AB1 and AB2(folders) |_AB1 and AB2
|_CD1 |__CD1 |__CD2
But it is checking for CD1 in both AB1 and AB2 folders and
files are not generating.
但它在AB1和AB2文件夹中都检查CD1,并且文件没有生成。
File file = new File("workspace");
List
abFolders = new ArrayList
List
//print
for (File file1 : abFolders) {
for(File file2:cdFolders){
//Here I need to check if CD folder is present in AB1 then it has to copy to CD1 and not to check for another CD1 folder in AB2 folder.
System.out.println(file1.getName());
System.out.println(file2.getName());
}
}
private static List
List
public boolean accept(File f) {
return f.isDirectory();
}
}));
return subdirs;
}
static List
List
for(File subdir : subdirs) {
deepSubdirs.addAll(getDir(subdir));
}
//subdirs.addAll(deepSubdirs);
return deepSubdirs;
}
英文:
I have folder structure as below and I need to check and copy files from AB1 folder to CD1 folder.
Root Folder
|___AB ------------------------>DD
|__AB1 and AB2(folders) |_AB1 and AB2
|_CD1 |__CD1 |__CD2
But it is checking for CD1 in both AB1 and AB2 folders and
files are not generating.
File file = new File("workspace");
List<File> abFolders = getDir(file); //level 1 folders
abFolders = new ArrayList<File>(abFolders);
List<File> cdFolders = getSubdirs(abFolders); //level 2 folders
//print
for (File file1 : abFolders) {
for(File file2:cdFolders){
//Here I need to check if CD folder is present in AB1 then it has to copy to CD1 and not to check for another CD1 folder in AB2 folder.
System.out.println(file1.getName());
System.out.println(file2.getName());
}
}
private static List<File> getDir(File file) {
List<File> subdirs = Arrays.asList(file.listFiles(new FileFilter() {
public boolean accept(File f) {
return f.isDirectory();
}
}));
return subdirs;
}
static List<File> getSubdirs(List<File> subdirs) {
List<File> deepSubdirs = new ArrayList<File>();
for(File subdir : subdirs) {
deepSubdirs.addAll(getDir(subdir));
}
//subdirs.addAll(deepSubdirs);
return deepSubdirs;
}
答案1
得分: 3
使用集合来对abFolders和cdFolders进行排序。首先导入库:
import java.util.Collections;
然后在进入循环之前对AB文件夹进行排序:
Collections.sort(abFolders);
for (File file1 : abFolders) {
for (File file2 : cdFolders) {
//在这里,我需要检查CD文件夹是否存在于AB1中,然后将其复制到CD1中,而不是检查AB2文件夹中的另一个CD1文件夹。
System.out.println(file1.getName());
System.out.println(file2.getName());
}
}
英文:
Use collections, so that you sort abFolders and cdFolders. First import the library:
import java.util.Collections;
Then do the sort of AB folders, before entering loop.
Collections.sort(abFolders);
for (File file1 : abFolders) {
for(File file2:cdFolders){
//Here I need to check if CD folder is present in AB1 then it has to copy to CD1 and not to check for another CD1 folder in AB2 folder.
System.out.println(file1.getName());
System.out.println(file2.getName());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论