java - 如何创建文件目录并包含文件数组中的文件

标签 java file

File.listRoots() 方法返回代表系统所有根文件夹的文件数组。我想创建一个逻辑文件并将所有这些根添加到其中,这样我就可以调用一个以 File 作为参数的方法。

例如,给定这个伪方法:

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

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

要求上面所示的伪代码中的roots是一个包含系统上所 Root过的逻辑文件。这可能吗?

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

   }

最佳答案

File 对象是系统上物理文件的表示,您不能任意创建“虚拟文件”,其中包含其他文件的分组,脱离底层操作系统的实际情况。最接近您想要做的事情可能需要在系统上创建一个文件夹,其中包含指向所有根目录的符号链接(symbolic link) - 您可以在 Java 7 中执行此操作。我的直觉告诉我您正在尝试与现有代码集成,位于我会问这一点 - 使用每个根文件一个接一个地调用函数会产生什么负面影响?

关于java - 如何创建文件目录并包含文件数组中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10151341/

相关文章:

java - Spring集成和持久对象

java - 执行删除和更新时停止

python - 明确关闭文件重要吗?

c - 从文件中读取行,仅打印与用户选择的字母数量相同的行

c++ - 测试目录 S_ISDIR 行为不一致

java - 有没有办法从Java中的FileDialog获取目录路径?

java - Eclipse UTF-8-奇怪的字符

java - 如何让System.err写在一个 block 中

c - 读取文件时无限循环

Java 通过套接字发送和接收文件 (byte[])