2012-04-14 5 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

당신이하려는 일이 명확하지 않습니다. 그 파일에 모든 뿌리를 추가하면 무엇을 의미합니까? 그들을 복사? 링크를 추가 하시겠습니까? –

+0

http://docs.oracle.com/javase/tutorial/essential/io/links.html –

+0

위의 예와 같이 파일 유형 변수를 원한다면 모든 루트가 있어야합니다. 그래서 나는 그 변수를 FileTreeModel 생성자에 전달하여 Tree 모델을 얻을 수있다. –

답변

1

File 객체가 시스템에 실제 파일의 표현입니다, 당신은 임의로 기본 운영 체제의 현실에서 이혼 그들에 다른 파일의 그룹과 '가상 파일'을 만들 수 없습니다. 가장 가까운 곳에서 당신이 찾고자하는 것은 아마도 뿌리에 대한 심볼릭 링크가있는 폴더를 시스템에 만들 것을 요구할 것입니다. 자바 7에서 할 수있는 일이 있습니다. 기존 코드와 통합하려고 시도하고 있습니다. 어떤 점을 물어볼 것입니다 - 각 루트 파일에 함수를 호출 할 때마다 부정적인 영향은 무엇입니까?

+0

위의 코드를 참조하십시오. 모든 드라이브에 해당 내용을 표시하려고합니다. 하지만 루프를 사용하여 FileTreeModel을 호출하면 다른 파일과 중복되어 마지막 드라이브 만 표시됩니다. 그게 y 야. –

관련 문제