2012-04-14 93 views
0

File.listRoots()方法返回表示系統的所有根文件夾的文件數組。我想創建一個邏輯文件,並將所有這些根添加到它,所以我可以調用一個方法,它需要File作爲它的參數。如何從文件數組創建文件目錄和包含的文件

例如,給定這種僞方法:

void xyz(final File f) { 
    // .... 
} 

final File roots = somekindofwrapper(File.listRoots()); 
xyz(roots); 

要求是roots在上面所示的僞碼是包含在系統上的所有根一個邏輯文件。這可能嗎?

import javax.swing.*; 
    import java.io.File; 
    public class FileTreeDemo { 



    public static void main(String[] args) { 

    try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     }catch(Exception e){ 
      System.out.println("cant done"); 
     } 
    // Create a JTree and tell it to display our model 
    JTree tree = new JTree(); 

    // The JTree can get big, so allow it to scroll 
    JScrollPane scrollpane = new JScrollPane(tree); 
     // Figure out where in the filesystem to start displaying 

    File[] roots = File.listRoots(); 
    FileTreeModel model = new FileTreeModel(null); 


    // for(File root: roots) 
    // { 
    model=new FileTreeModel(roots[0]); 
     tree.setModel(model); 
    // } 




    JFrame frame = new JFrame("FileTreeDemo"); 
    frame.getContentPane().add(scrollpane, "Center"); 
    frame.setSize(400,600); 
    frame.setVisible(true); 

    } 

    } 


    import java.io.File; 

    import javax.swing.event.TreeModelListener; 
    import javax.swing.tree.DefaultTreeSelectionModel; 
    import javax.swing.tree.TreeModel; 
    import javax.swing.tree.TreePath; 
    import javax.swing.tree.TreeSelectionModel; 

    public class FileTreeModel extends DefaultTreeSelectionModel implements TreeModel{ 
    // We specify the root directory when we create the model. 

    protected File root; 
    public FileTreeModel(File root) { this.root = root; 
    //setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); 
    } 

    // The model knows how to return the root object of the tree 
    public Object getRoot() { return root; } 

    // Tell JTree whether an object in the tree is a leaf 
    public boolean isLeaf(Object node) { return ((File)node).isFile(); } 

    // Tell JTree how many children a node has 
    public int getChildCount(Object parent) { 
    String[] children = ((File)parent).list(); 
    if (children == null) return 0; 
    return children.length; 
    } 

    // Fetch any numbered child of a node for the JTree. 
    // Our model returns File objects for all nodes in the tree. The 
    // JTree displays these by calling the File.toString() method. 
    public Object getChild(Object parent, int index) { 
    String[] children = ((File)parent).list(); 
    if ((children == null) || (index >= children.length)) return null; 
    return new File((File) parent, children[index]); 
    } 

    // Figure out a child's position in its parent node. 
    public int getIndexOfChild(Object parent, Object child) { 
    String[] children = ((File)parent).list(); 
    if (children == null) return -1; 
    String childname = ((File)child).getName(); 
    for(int i = 0; i < children.length; i++) { 
    if (childname.equals(children[i])) return i; 
    } 
    return -1; 
    } 

    // This method is invoked by the JTree only for editable trees. 
    // This TreeModel does not allow editing, so we do not implement 
    // this method. The JTree editable property is false by default. 
    public void valueForPathChanged(TreePath path, Object newvalue) {} 

    // Since this is not an editable tree model, we never fire any events, 
    // so we don't actually have to keep track of interested listeners*/ 
    public void addTreeModelListener(TreeModelListener l) {} 
    public void removeTreeModelListener(TreeModelListener l) {} 

    } 
+1

目前尚不清楚你想要做什麼。將所有的根添加到該文件中意味着什麼?複製它們?添加鏈接? – 2012-04-14 05:49:44

+0

http://docs.oracle.com/javase/tutorial/essential/io/links.html – 2012-04-14 05:50:16

+0

我也不確定,但根據上面的例子我想要一個文件類型的變量應該有所有的根系統,所以我可以將該變量傳遞給FileTreeModel構造函數以獲取樹模型。 – 2012-04-16 06:44:22

回答

1

File對象是系統上的物理文件的表示,你不能隨心所欲地創造「虛擬文件」,目標在他們的其他文件,從底層操作系統的閉門造車分組。最接近你想要做的事情就是需要在系統上創建一個文件夾,並在所有根上建立符號鏈接 - 在Java 7中你可以做的事情。我的直覺告訴我你正試圖與現有的代碼集成,哪一點我會問 - 每個根文件一個接一個地調用你的函數的負面影響是什麼?

+0

看到上面的代碼我試圖顯示所有驅動器n其內容。但如果我打電話FileTreeModel使用循環其重疊其他一個和只有最後一個驅動器是可見的。那是你。 – 2012-04-14 14:57:28

相關問題