我自己实现了挑战的解决方案,它可以作为 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=[]}]}]}