(.)(.) 百分求解, 如何获得本地系统的根目录(也就是C D E 盘的再上一级目录), 作为树形结构的根结点我在用Java实现资源管理器(左为一数据树,  遍历本地系统中的文件结构,  右为一JTextArea,  显示选中节点为.xml  或  .txt的内容)  
 
我已实现对指定盘的数据遍历,  但不知如何获得本地系统的根目录(也就是C  D  E  盘的再上一级目录),  以达到遍历整个系统的目的.  
 
E盘目录结构的遍历  
import  javax.swing.*;  
import  javax.swing.event.*;  
import  javax.swing.tree.*;  
import  java.awt.*;  
import  java.awt.event.*;  
import  java.io.File;  
import  java.util.EventObject;  
 
public  class  Test  extends  JFrame  {  
           public  Test()  {  
                       final  JTree  tree  =  new  JTree(createTreeModel());  
                       JScrollPane  l_scrollPane  =  new  JScrollPane(tree);  
                       getContentPane().add(l_scrollPane,  BorderLayout.WEST);  
                         
                       final  JTextArea  textArea  =  new  JTextArea(10,10);  
                       JScrollPane  r_scrollPane  =  new  JScrollPane(textArea);  
                       getContentPane().add(r_scrollPane,  BorderLayout.CENTER);                          
                         
                       //getContentPane().add(GJApp.getStatusArea(),  BorderLayout.SOUTH);  
 
                       tree.addTreeExpansionListener(new  TreeExpansionListener(){  
                                   public  void  treeCollapsed(TreeExpansionEvent  e)  {  
                                   }  
                                   public  void  treeExpanded(TreeExpansionEvent  e)  {  
                                               UpdateStatus  updateThread;  
                                               TreePath  path  =  e.getPath();  
                                               FileNode  node  =  (FileNode)  
                                                                                                     path.getLastPathComponent();  
 
                                               if(  !  node.isExplored())  {  
                                                           DefaultTreeModel  model  =  (DefaultTreeModel)tree.getModel();  
                                                           GJApp.updateStatus("exploring  ...");  
 
                                                           UpdateStatus  us  =  new  UpdateStatus();  
                                                           us.start();  
 
                                                           node.explore();  
                                                           model.nodeStructureChanged(node);  
                                               }  
                                   }  
                                   class  UpdateStatus  extends  Thread  {  
                                               public  void  run()  {  
                                                           try  {  Thread.currentThread().sleep(450);  }  
                                                           catch(InterruptedException  e)  {  }  
 
                                                           SwingUtilities.invokeLater(new  Runnable()  {  
                                                                       public  void  run()  {  
                                                                                   GJApp.updateStatus("  ");  
                                                                       }  
                                                           });  
                                               }  
                                   }  
                       });  
           }  
           private  DefaultTreeModel  createTreeModel()  {  
                       File  root  =  new  File("E:/");  
                       FileNode  rootNode  =  new  FileNode(root);  
 
                       rootNode.explore();  
                       return  new  DefaultTreeModel(rootNode);  
           }  
           public  static  void  main(String  args[])  {  
                       GJApp.launch(new  Test(),"JTree  File  Explorer",  
                                                                                                 300,300,450,400);  
           }  
}  
class  FileNode  extends  DefaultMutableTreeNode  {  
           private  boolean  explored  =  false;  
 
           public  FileNode(File  file)              {    
                       setUserObject(file);    
           }  
           public  boolean  getAllowsChildren()  {  return  isDirectory();  }  
           public  boolean  isLeaf()              {  return  !isDirectory();  }  
           public  File  getFile()                        {  return  (File)getUserObject();  }  
 
           public  boolean  isExplored()  {  return  explored;  }  
 
           public  boolean  isDirectory()  {  
                       File  file  =  getFile();  
                       return  file.isDirectory();  
           }  
           public  String  toString()  {  
                       File  file  =  (File)getUserObject();  
                       String  filename  =  file.toString();  
                       int  index  =  filename.lastIndexOf(File.separator);  
 
                       return  (index  !=  -1  &&  index  !=  filename.length()-1)  ?    
                                                                                                           filename.substring(index+1)  :    
                                                                                                           filename;  
           }  
           public  void  explore()  {  
                       if(!isDirectory())  
                                   return;  
 
                       if(!isExplored())  {  
                                   File  file  =  getFile();  
                                   File[]  children  =  file.listFiles();  
 
                                   for(int  i=0;  i  <  children.length;  ++i)    
                                               add(new  FileNode(children[i]));  
 
                                   explored  =  true;  
                       }  
           }  
}  
class  GJApp  extends  WindowAdapter  {  
           static  private  JPanel  statusArea  =  new  JPanel();  
           static  private  JLabel  status  =  new  JLabel("  ");  
 
           public  static  void  launch(final  JFrame  f,  String  title,  
                                                                                       final  int  x,  final  int  y,    
                                                                                       final  int  w,  int  h)  {  
                       f.setTitle(title);  
                       f.setBounds(x,y,w,h);  
                       f.setVisible(true);  
 
                       statusArea.setBorder(BorderFactory.createEtchedBorder());  
                       statusArea.setLayout(new  FlowLayout(FlowLayout.LEFT,0,0));  
                       statusArea.add(status);  
                       status.setHorizontalAlignment(JLabel.LEFT);  
 
                       f.setDefaultCloseOperation(  
                                                                                   WindowConstants.DISPOSE_ON_CLOSE);  
 
                       f.addWindowListener(new  WindowAdapter()  {  
                                   public  void  windowClosed(WindowEvent  e)  {  
                                               System.exit(0);  
                                   }  
                       });  
           }  
           static  public  JPanel  getStatusArea()  {  
                       return  statusArea;  
           }  
           static  public  void  updateStatus(String  s)  {  
                       status.setText(s);  
           }  
}

解决方案 »

  1.   

    获得给定目录上级目录名可以用
    getParentFile() 

    getParent()
    具体用法请参考API文档但是像E:\,其上级目录为空,是没有办法得到的你可以建立一个虚拟的节点为根,比如名字叫:我的电脑
    这个根节点下面的就是硬盘的盘符,
    可以用下面的方法得到
            File file[] = File.listRoots();
            for(int i=0;i<file.length;i++)
                System.out.println(file[i].getCanonicalPath());
      

  2.   

    代码简单,运行速度超级慢,
    使用了递归的方法,
    导致没一次初始化都是对整个计算机的文件进行全搜索!!/*
     * Created on 2005-5-31
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package com.tree;import java.awt.Container;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.File;import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;/**
     * @author Cinda
     * 
     * TODO To change the template for this generated type comment go to Window -
     * Preferences - Java - Code Style - Code Templates
     */
    public class ArrayTree {    public ArrayTree() {
            JFrame f = new JFrame("TreeDemo");
            Container contentPane = f.getContentPane();
            JScrollPane scrollPane = new JScrollPane();        File d1 = new File("d:");
            DefaultMutableTreeNode computer = new DefaultMutableTreeNode("我的电脑" );
            DefaultMutableTreeNode c=new DefaultMutableTreeNode("本地磁盘(c:)");
            DefaultMutableTreeNode d=new DefaultMutableTreeNode("本地磁盘(d:)");
            DefaultMutableTreeNode e=new DefaultMutableTreeNode("本地磁盘(e:)");
            DefaultMutableTreeNode ff=new DefaultMutableTreeNode("本地磁盘(f:)");
            showNotes(new File("c:"),c);
            showNotes(new File("d:"),d);
            showNotes(new File("e:"),e);
            showNotes(new File("f:"),ff);
            computer.add(c);
            computer.add(d);
            computer.add(e);
            computer.add(ff);
            
            JTree tree = new JTree(computer);
            scrollPane.setViewportView(tree);
            contentPane.add(scrollPane);
            f.pack();
            f.setVisible(true);
            f.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }    public static void main(String[] args) {
            new ArrayTree();    }
        
        public static void showNotes(File f, DefaultMutableTreeNode root) {
            for (int i = 0; i < f.listFiles().length; i++) {
                if (f.listFiles()[i].isDirectory()) {
                    DefaultMutableTreeNode temp=new DefaultMutableTreeNode(f.listFiles()[i].getPath()); 
                    root.add(temp);
                   // System.out.println(f.listFiles()[i].getPath());
                    showNotes(f.listFiles()[i],temp);
                } else{
                    root.add(new DefaultMutableTreeNode(f.listFiles()[i].getName()));
                }
            }
        }
        
        
    }给你贴一个我收藏的,呵呵
      

  3.   

    所谓“我的电脑”也就是C盘、D盘的上一级,只是Windows系统中的概念,对应的是系统中的一个核心对象,而并不是文件系统中的一个概念。因此,你通过文件系统的规则肯定无法得到这个位置。(也就是说它并不是位置,你怎么遍历到它?)。
    例如在与Windows同机安装的Linux上可以Mount到C盘或D盘为一个卷,可是它却无法处理“我的电脑”。
    如果非要加上“我的电脑”这层,那你就要在文件系统组织结构之外,加上自己的逻辑了。按照这个原则,你不但可以遍历到“我的电脑”,还可以到“网上邻居”,浏览网络共享的文件呢。
      

  4.   

    我原本想读C盘的上级目录, 因为我认为C盘每个机子都有, 看来还是不行.那现在我的问题是如何实现 用Java实现资源管理器(左为一数据树,  遍历本地系统中的文件结构,  右为一JTextArea,  显示选中节点为.xml  或  .txt的内容)那位大哥曾做过, 或有相关的示例请发送至  [email protected]  先谢了
      

  5.   

    jFresH_MaN(Most Vacant Player)
    你这个程序根本不实用,
    建议一次搜索到下一级,而不是全部遍历,然后增加鼠标事件,按需进行下一级的添加