這部分類負責創建目錄和檔案并附加它。
public class TreeNode<T> implements Iterable<TreeNode<T>> {
public T data;
public TreeNode<T> parent;
public List<TreeNode<T>> children;
public boolean isRoot() {
return parent == null;
}
private List<TreeNode<T>> elementsIndex;
public TreeNode(T data) {
this.data = data;
this.children = new LinkedList<TreeNode<T>>();
this.elementsIndex = new LinkedList<TreeNode<T>>();
this.elementsIndex.add(this);
}
public TreeNode<T> addChild(T child) {
TreeNode<T> childNode = new TreeNode<T>(child);
childNode.parent = this;
this.children.add(childNode);
this.registerChildForSearch(childNode);
return childNode;
}
private void registerChildForSearch(TreeNode<T> node) {
elementsIndex.add(node);
if (parent != null)
parent.registerChildForSearch(node);
}
@Override
public String toString() {
return data != null ? data.toString() : "[data null]";
}
@Override
public Iterator<TreeNode<T>> iterator() {
TreeNode<T> iter = new TreeNode<T>((T) this);
return (Iterator<TreeNode<T>>) iter;
}
public static TreeNode<File> createDirTree(File folder) {
if (!folder.isDirectory()) {
throw new IllegalArgumentException("folder is not a Directory");
}
TreeNode<File> DirRoot = new TreeNode<File>(folder);
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
appendDirTree(file, DirRoot);
} else {
appendFile(file, DirRoot);
}}
return DirRoot;
}
public static void appendDirTree(File folder, TreeNode<File> DirRoot) {
DirRoot.addChild(folder);
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
appendDirTree(file, DirRoot.children.get(DirRoot.children.size() - 1));
} else {
appendFile(file, DirRoot.children.get(DirRoot.children.size() - 1));
}} }
public static void appendFile(File file, TreeNode<File> filenode) {
filenode.addChild(file);
}
問題是在這種代碼狀態下,檔案和目錄的列印方式如下:
rootFolder
├─ Folder1
│ ├─ subFolder1
│ │ ├─ a.txt
│ │ ├─ b.txt
│ │ ├─ c.txt
│ │ └─ d.txt
│ ├─ subFolder2
│ │ ├─ a.txt
│ │ ├─ B.txt
│ │ ├─ c.txt
│ │ └─ D.txt
│ ├─ subFolder3
│ │ ├─ A.txt
│ │ ├─ b.txt
│ │ ├─ C.txt
│ │ └─ d.txt
│ └─ subFolder4
│ ├─ a.txt
│ ├─ b.txt
│ ├─ c.txt
│ └─ d.txt
├─ File1.txt
├─ Folder2
│ ├─ a.txt
│ ├─ b.txt
│ ├─ c.txt
│ └─ d.txt
└─ File2.txt
但我需要先去目錄,然后是檔案:File1.txtnear File2.txt。我試圖找出問題出在哪里,但無濟于事。我需要更改什么以及在代碼中的何處獲得所需的結果?
uj5u.com熱心網友回復:
問題在于您附加子樹的順序。
正如Java 檔案所說的.listFiles():
無法保證結果陣列中的名稱字串會以任何特定順序出現;特別是,它們不能保證按字母順序出現。
因此,您需要在附加檔案之前對檔案進行排序。
這是方法createDirTree和的可能解決方案appendDirTree。
(注意:排序僅基于是否為s a directory or not. If you need another custom sorting, you need to adapt the Comparator`)
public static TreeNode<File> createDirTree(File folder) {
if (!folder.isDirectory()) {
throw new IllegalArgumentException("folder is not a Directory");
}
List<File> children = Arrays.asList(folder.listFiles());
children.sort(Comparator.comparing(file -> file.isDirectory() ? -1 : 1));
TreeNode<File> DirRoot = new TreeNode<File>(folder);
for (File file : children) {
if (file.isDirectory()) {
appendDirTree(file, DirRoot);
} else {
appendFile(file, DirRoot);
}}
return DirRoot;
}
public static void appendDirTree(File folder, TreeNode<File> dirRoot) {
dirRoot.addChild(folder);
List<File> children = Arrays.asList(folder.listFiles());
children.sort(Comparator.comparing(file -> file.isDirectory() ? -1 : 1));
for (File file : children) {
if (file.isDirectory()) {
appendDirTree(file, dirRoot.children.get(dirRoot.children.size() - 1));
} else {
appendFile(file, dirRoot.children.get(dirRoot.children.size() - 1));
}
}
}
有幾種方法可以實作這一點。例如,您還可以使用流。請參閱以下示例createDirTree:
public static TreeNode<File> createDirTree(File folder) {
if (!folder.isDirectory()) {
throw new IllegalArgumentException("folder is not a Directory");
}
TreeNode<File> DirRoot = new TreeNode<File>(folder);
Arrays.stream(folder.listFiles())
.sorted(Comparator.comparing(f -> (f.isDirectory() ? 1 : -1)))
.forEach(file -> {
if (file.isDirectory()) {
appendDirTree(file, DirRoot);
} else {
appendFile(file, DirRoot);
}
});
return DirRoot;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/367714.html
