如何在Java中检查同一级别的子文件夹中是否存在文件夹?

huangapple go评论145阅读模式
英文:

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 = getDir(file); //level 1 folders
abFolders = new ArrayList(abFolders);
List 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 getDir(File file) {
List subdirs = Arrays.asList(file.listFiles(new FileFilter() {
public boolean accept(File f) {
return f.isDirectory();
}
}));
return subdirs;
}
static List getSubdirs(List subdirs) {
List deepSubdirs = new ArrayList();
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());
    }
    
}

huangapple
  • 本文由 发表于 2020年8月13日 15:39:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63390346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定