查找所有文件夹和子文件夹内容并显示为结构(Java)

时间:2013-11-12 10:35:54

标签: java

我正在开发一个项目,要求我获取所有文件的名称和文件夹(子文件夹)名称。 我试图通过在我的第一种方法中使用名为yo的ArrayList来实现它。

开头的代码如下:

public ArrayList varToList(File folder) {

        for(File fileEntry : folder.listFiles()){
            if(fileEntry.isDirectory()){
                yo.add(fileEntry.getName());
                varToList(fileEntry);
            } else
                yo.add(fileEntry.getName());
        }
        return yo;
    }

输出从开始执行代码之后:

[AddFilesDeflateComp.zip, AddFilesDeflateComp1.zip, AddFolder.zip, HAHAFolder, 200HAHA.txt, YO.txt, HAHAFolder.zip, RootText1.txt, RootText2.txt, RootText3.txt, test.zip, TestFolderA, TestFolderA1, text1.txt, Text2.txt, Text3.txt, TextA.txt, TextB.txt, TestFolderB, TestFolderC, TestFolder 4, boss.txt, New folder, play you.txt]

然后我切换到另一种方法,因为上面显然无法做到。

以下是获取文件结构的代码:

String folderName = "root";
ArrayList<String> folders = new ArrayList<String>();
ArrayList<String> files = new ArrayList<String>();
ArrayList<String> previousFiles = new ArrayList<String>();
Map<String, ArrayList> map2 = new LinkedHashMap<String, ArrayList>();
Map<String, ArrayList> map = new LinkedHashMap<String, ArrayList>();

public void listFilesForFolder(File folder) {
    ArrayList<String> no = new ArrayList<String>();
    files = new ArrayList<String>();
    for (File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
            previousFiles = files;
            map2.put(folderName, previousFiles);
            folders.add(folderName);
            folderName = fileEntry.getName();
            listFilesForFolder(fileEntry);
            if (folders.size() - 1 != -1) {
                folderName = folders.get(folders.size() - 1);
                folders.remove(folders.get(folders.size() - 1));
                files = map2.get(folderName);

            }
        } else {
            files.add(fileEntry.getName());
            map.put(folderName, files);
        }
    }

}

地图的最终内容如下:

{root=[AddFilesDeflateComp.zip, AddFilesDeflateComp1.zip, AddFolder.zip, HAHAFolder.zip, RootText1.txt, RootText2.txt, RootText3.txt, test.zip], HAHAFolder=[200HAHA.txt, YO.txt], TestFolderA1=[text1.txt, Text2.txt, Text3.txt], TestFolderA=[TextA.txt, TextB.txt], TestFolder 4=[boss.txt, play you.txt]}

我的预期输出:

{root=[AddFilesDeflateComp.zip, AddFilesDeflateComp1.zip, AddFolder.zip, HAHAFolder.zip, RootText1.txt, RootText2.txt, RootText3.txt, test.zip],HAHAFolder=[200HAHA.txt, YO.txt],TestFolderA=[TextA.txt,TextB.txt,TestFolderA1=[text1.txt,Text2.txt,Text3.txt],TestFolderB=[TestFolderC=[TestFolder 4=[New folder,boss.txt,play you.txt]]]]}

以上代码可以获取子文件夹的内容,并且可以获取文件夹的名称但不是空的子文件夹或文件夹

执行上述代码时的文件结构:

Root/
HAHAFolder
    /200HAHA.txt
    /Yo.txt
TestFolderA
    /TestFolderA1
        /text1.txt
        /Text2.txt
        /Text3.txt
    /TextA.txt
    /TextB.txt
TestFolderB
    /TestFolderC
        /TestFolder 4
            /New folder
            /boss.txt
            /play you.txt
AddFilesDeflateComp.zip
AddFilesDeflateComp1.zip
AddFolder.zip
HAHAFolder.zip
RootText1.txt
RootText2.txt
RootText3.txt
test.zip

是否有任何其他方法或如何修改上述代码以获得我需要的

1 个答案:

答案 0 :(得分:0)

递归apporach最适合文件系统的递归结构,恕我直言。

您的第一种方法并不是那么糟糕,您只需要一个List参数来添加您找到的项目:

public List<File> addToList(File item, List<File> list) {
     list.add(item);
     if (item.isDirectory()) {
        for(File fileEntry : item.listFiles())
            list = addToList(fileEntry, list);
     }
     return list;
}

你称之为:

  File topdir = ....;  
  List<File> all = addToList(topdir, new ArrayList<File>());

如果您只想打印目录结构:

public void printTree(File item, int depth) {
     for (i=0; i< depth; i++) System.out.print(" ");
     System.out.println (item);
     if (item.isDirectory()) {
        for(File fileEntry : item.listFiles())
            printTree(fileEntry, depth+2);
     }
}