gpt4 book ai didi

java - 从 CSV 文件实例化 N 叉树

转载 作者:行者123 更新时间:2023-12-01 08:46:23 28 4
gpt4 key购买 nike

我正在编写文件系统层次结构的 N 叉树表示形式,其中每个节点都包含有关它所表示的文件/文件夹的一些信息。

public class TreeNode {

private FileSystemEntry data;
private boolean directory;
private TreeNode parent;
private ArrayList<TreeNode> children;
private int numChildren;
private int key;

树被存储为它自己的对象,因为我要比较几棵树

public class DirectoryTree {

private TreeNode Root;
private int numNodes;
private TreeNode Focus;
private LocalDateTime date;
private long totalSizeOnDisk;

这是供引用的 FileSystemEntry 类

public class FileSystemEntry {

private Path path;
private String name;
private long sizeOnDisk;
private FileTime created;
private FileTime lastModified;

我需要将每个节点写入一个文件,以便以后可以重新加载它,以便比较两个不同时间的树。我可以轻松地将 FileSystemEntry 中的变量和 DirectoryTree 中的日期写入 CSV,以便重新创建每个 FSE 及其各自的节点,但我无法理解如何确定哪个节点是给定节点父级,以便重建树。

乍一看,广度优先遍历和 numChildren 甚至 TreeNode 中的键似乎是足够的信息,但我无法实现解决方案。我的想法是否正确,或者我是否使问题过于复杂化

最佳答案

使用ObjectOutputStreamDirectoryTree对象写入文件。因此,您必须为要编写的所有类实现 Serialized 接口(interface)。

这将保留您的结构/层次结构,并且您不必弄乱节点之间的关系。

示例:

public class TreeNode imlpements Serializable {

private FileSystemEntry data;
private boolean directory;
private TreeNode parent;
private ArrayList<TreeNode> children;
private int numChildren;
private int key;

}

public class DirectoryTree imlpements Serializable {

private TreeNode Root;
private int numNodes;
private TreeNode Focus;
private LocalDateTime date;
private long totalSizeOnDisk;

}

public class FileSystemEntry imlpements Serializable {

private Path path;
private String name;
private long sizeOnDisk;
private FileTime created;
private FileTime lastModified;

}

将对象写入文件:

public class IOTest {

public static void main(String[] args) {

File file;

// create your Tree
DirectoryTree dirTree;
file = new File("filename");
dirTree = createTree();

// write object into file
writeTree( dirTree, file );

// read object from file
DirectoryTree newDirTree;
file = new File("filename");
newDirTree = readTree( file );

}

public static DirectoryTree createTree() {

// create here your tree

}

public static void writeTree( DirectoryTree tree, File file ) {

FileOutputStream fos;
ObjectOutputStream oos;

try {
fos = new FileOutputStream( file );
oos = new ObjectOutputStream( fos );

oos.writeObject( tree );
oos.flush();

oos.close();
} catch( IOException ioe ) {
ioe.printStackTrace();
}

}

public static DirectoryTree readTree( File file ) {

DirectoryTree loadedTree = null;
FileInputStream fos;
ObjectInputStream oos;

try {
fos = new FileInputStream( file );
oos = new ObjectInputStream( fos );


// you have to cast your object into a DirectoryTree,
// because read() will return an object of type `Object`
loadedTree = (DirectoryTree) oos.read();

oos.close();
} catch( IOException ioe ) {
ioe.printStackTrace();
}

return loadedTree;

}

}

关于java - 从 CSV 文件实例化 N 叉树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43022433/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com