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) {}

   }
4

1 に答える 1

1

The File object is a representation of a physical file on the system, you can't arbitrarily create 'virtual files' with groupings of other files in them, divorced from the reality of the underlying operating system. The closest you could probably come to what you are looking to do would require creating a folder on the system with symlinks to all the roots - something you can do in Java 7. My gut tells me you are trying to integrate with existing code, at which point I will ask - what is the negative impact of calling your function with each root file, one after the other?

于 2012-04-14T06:10:03.587 に答える