Java Tree to represent filesystem (files/dir) from list of paths

前端 未结 5 688
感动是毒
感动是毒 2021-02-04 02:39

I\'ve a list of paths like this

/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/sdc         


        
5条回答
  •  [愿得一人]
    2021-02-04 03:06

    I would recommend reading up on data structures, particularly trees. In Java, you can represent these by creating a node class that has references to other nodes. For example:

    public class Node {
        private Node[] children;
        /* snip other fields */
        public boolean isFile() {
             return children.count == 0;
        }
    }
    

    Obviously, you can store your node references anyway you like- arrays or collections will work with non-binary trees.

    Given your list of files, you can read these and populate your tree structure.

提交回复
热议问题