我在用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.   

    我要把本地系统的根目录(也就是C D E 盘的再上一级目录)作为根节点
    File root = new File("E:/");   E:/  是盘符
    File类中有获得给定目录的上级目录名的方法吗?
      

  2.   

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

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

  3.   

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