从路径列表中表示文件系统(文件/目录)的 Java 树

新手上路,请多包涵

我有一个这样的路径列表

/mnt/sdcard/folder1/a/b/file1
/mnt/sdcard/folder1/a/b/file2
/mnt/sdcard/folder1/a/b/file3
/mnt/sdcard/folder1/a/b/file4
/mnt/sdcard/folder1/a/b/file5
/mnt/sdcard/folder1/e/c/file6
/mnt/sdcard/folder2/d/file7
/mnt/sdcard/folder2/d/file8
/mnt/sdcard/file9

因此,从这个路径列表 (Stings) 中,我需要创建一个 Java 树结构,该结构将文件夹作为节点,将文件作为叶(不会有空文件夹作为叶)。

我认为我需要的是 add 方法,我将一个字符串(文件的路径)传递给它们,并将其添加到树中的正确位置,如果它们还不存在,则创建正确的节点(文件夹)

当我在节点和叶子列表上时,这个树结构需要我获取节点列表(但我认为这将是树的正常功能)

我将始终将字符串作为路径,而不是真正的文件或文件夹。是否有现成的东西或源代码可以启动?

非常感谢。

原文由 StErMi 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 506
2 个回答

谢谢你的回答。我做了我的工作实施。我认为我需要改进它,以便在向树结构添加元素时使用更多缓存更好地工作。

正如我所说,我需要的是一种结构,它允许我对 FS 进行“虚拟”表示。

MXMTree.java

 public class MXMTree {

    MXMNode root;
    MXMNode commonRoot;

    public MXMTree( MXMNode root ) {
        this.root = root;
        commonRoot = null;
    }

    public void addElement( String elementValue ) {
        String[] list = elementValue.split("/");

        // latest element of the list is the filename.extrension
        root.addElement(root.incrementalPath, list);

    }

    public void printTree() {
        //I move the tree common root to the current common root because I don't mind about initial folder
        //that has only 1 child (and no leaf)
        getCommonRoot();
        commonRoot.printNode(0);
    }

    public MXMNode getCommonRoot() {
        if ( commonRoot != null)
            return commonRoot;
        else {
            MXMNode current = root;
            while ( current.leafs.size() <= 0 ) {
                current = current.childs.get(0);
            }
            commonRoot = current;
            return commonRoot;
        }

    }

}

MXMNode.java

 import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MXMNode {

    List<MXMNode> childs;
    List<MXMNode> leafs;
    String data;
    String incrementalPath;

    public MXMNode( String nodeValue, String incrementalPath ) {
        childs = new ArrayList<MXMNode>();
        leafs = new ArrayList<MXMNode>();
        data = nodeValue;
        this. incrementalPath = incrementalPath;
    }

    public boolean isLeaf() {
        return childs.isEmpty() && leafs.isEmpty();
    }

    public void addElement(String currentPath, String[] list) {

        //Avoid first element that can be an empty string if you split a string that has a starting slash as /sd/card/
        while( list[0] == null || list[0].equals("") )
            list = Arrays.copyOfRange(list, 1, list.length);

        MXMNode currentChild = new MXMNode(list[0], currentPath+"/"+list[0]);
        if ( list.length == 1 ) {
            leafs.add( currentChild );
            return;
        } else {
            int index = childs.indexOf( currentChild );
            if ( index == -1 ) {
                childs.add( currentChild );
                currentChild.addElement(currentChild.incrementalPath, Arrays.copyOfRange(list, 1, list.length));
            } else {
                MXMNode nextChild = childs.get(index);
                nextChild.addElement(currentChild.incrementalPath, Arrays.copyOfRange(list, 1, list.length));
            }
        }
    }

    @Override
    public boolean equals(Object obj) {
        MXMNode cmpObj = (MXMNode)obj;
        return incrementalPath.equals( cmpObj.incrementalPath ) && data.equals( cmpObj.data );
    }

    public void printNode( int increment ) {
        for (int i = 0; i < increment; i++) {
            System.out.print(" ");
        }
        System.out.println(incrementalPath + (isLeaf() ? " -> " + data : "")  );
        for( MXMNode n: childs)
            n.printNode(increment+2);
        for( MXMNode n: leafs)
            n.printNode(increment+2);
    }

    @Override
    public String toString() {
        return data;
    }

}

Test.java 用于测试代码

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        String slist[] = new String[] {
                "/mnt/sdcard/folder1/a/b/file1.file",
                "/mnt/sdcard/folder1/a/b/file2.file",
                "/mnt/sdcard/folder1/a/b/file3.file",
                "/mnt/sdcard/folder1/a/b/file4.file",
                "/mnt/sdcard/folder1/a/b/file5.file",
                "/mnt/sdcard/folder1/e/c/file6.file",
                "/mnt/sdcard/folder2/d/file7.file",
                "/mnt/sdcard/folder2/d/file8.file",
                "/mnt/sdcard/file9.file"
        };

        MXMTree tree = new MXMTree(new MXMNode("root", "root"));
        for (String data : slist) {
            tree.addElement(data);
        }

        tree.printTree();
    }

}

如果您对改进有一些好的建议,请告诉我 :)

原文由 StErMi 发布,翻译遵循 CC BY-SA 3.0 许可协议

我自己实现了一个挑战解决方案,它 可以作为 GitHubGist 获得

我在 DirectoryNode 中表示文件系统层次结构的每个节点。辅助方法 createDirectoryTree(String[] filesystemList) 创建目录树。

这是 GitHubGist 中包含的用法示例。

 final String[] list = new String[]{
  "/mnt/sdcard/folder1/a/b/file1.file",
  "/mnt/sdcard/folder1/a/b/file2.file",
  "/mnt/sdcard/folder1/a/b/file3.file",
  "/mnt/sdcard/folder1/a/b/file4.file",
  "/mnt/sdcard/folder1/a/b/file5.file",
  "/mnt/sdcard/folder1/e/c/file6.file",
  "/mnt/sdcard/folder2/d/file7.file",
  "/mnt/sdcard/folder2/d/file8.file",
  "/mnt/sdcard/file9.file"
};

final DirectoryNode directoryRootNode = createDirectoryTree(list);

System.out.println(directoryRootNode);

System.out.println -输出是:

   {value='mnt', children=[{value='sdcard', children=[{value='folder1',
  children=[{value='a', children=[{value='b', children=[{value='file1.file',
  children=[]}, {value='file2.file', children=[]}, {value='file3.file',
  children=[]}, {value='file4.file', children=[]}, {value='file5.file',
  children=[]}]}]}, {value='e', children=[{value='c',
  children=[{value='file6.file', children=[]}]}]}]},
  {value='folder2', children=[{value='d', children=[{value='file7.file',
  children=[]}, {value='file8.file', children=[]}]}]},
  {value='file9.file', children=[]}]}]}

原文由 Markus Schulte 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题