2017-03-25 45 views
0

我正在對文件系統層次結構的N元樹表示進行編碼,其中每個節點都包含有關它所代表的文件/文件夾的一些信息。從CSV文件實例化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中的Date變量寫入CSV中,以便重新創建每個FSE及其各自的節點,但是我無法將自己的頭腦包圍,然後我會確切知道哪個節點是給定的節點父母,以重建樹。

乍一看,寬度第一次遍歷和從TreeNode的numChildren甚至鍵似乎是足夠的信息,但我一直沒有能夠實現一個解決方案。我是沿着正確的路線思考還是我在複雜化問題

回答

1

使用ObjectOutputStream將您的DirectoryTree對象寫入文件。因此,您必須將Serializable界面實施到您想要編寫的所有類。

這將保留你的結構/層次結構,而且你不必亂搞節點之間的關係。

實施例:

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; 

    } 

} 
+0

哇!不知道那是一件事!看起來比我想要做的更簡單。想到它,當然這是一件事哈哈。謝謝!我會給它一個去找回你 – Richardweber32

+1

適合與我的代碼很好,但我得到一個有趣的IOException:NotSerializableException:java.nio.file.attribute.FileTime 我假設這意味着FileTime對象不可序列化? – Richardweber32

+1

你認爲在節點中存儲created和lastModified會更容易,所以它們可以被序列化,並且如果我需要它們,可以轉換爲FileTime,或者在我將它們寫入文件之前將它們轉換爲很長時間,然後返回再次 – Richardweber32