我要做一个JTree,显示从系统根目录开始浏览本地文件(就像windows资源管理器默认的那样),应该怎么弄?
据我所知,
new File("d:\\")
new File(File.seperator)
等等都不是,高手来救救火啊,万分感谢。

解决方案 »

  1.   

    File root=new File("d:\\");void printFileList(File file){
      if(!file.exit()){
         return;
      }
      File fs[]=file.listFiles();
      for(int i=0;i<fs.length;i++){
         File f=fs[i];
         if(f.isDirectory()){
            printFileList(f);
         }else{
           System.out.println(f.getName());
         }
      }
    }简单的样例代码,没有在编译器下写,应该有语法错误,你看懂就可以了
      

  2.   

    上面是遍历文件的方法
    把文件遍历出来,放到JTree上你自己就可以实现了吧
      

  3.   

    package org.luyang.io;import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.StringReader;
    import java.net.URI;import org.luyang.lang.RegularExpressions;public class FilePathRead {
        public static void main(String[] args){
            FilePathRead t = new FilePathRead();
            t.find(new File("C:\\j2sdk1.4.2_10"));
        }
        
        public void find(File f){
            if(f.isDirectory()){
                File[] fileList = f.listFiles();
                for(int i = 0; i < fileList.length; i++){
                    find(fileList[i]);
                }
            }else{
               if(f.getName().endsWith(".java"))
                    System.out.println(f.getAbsolutePath());
            }
        }
    }
      

  4.   

    晕啊首先谢谢大家的热情捧场,在下非常感激。
    但是可能还是误会了,我要的不是怎么去实现JTree,我只是不知道怎么从系统根目录开始
    比方说这样子 new File("d:\\") 我已经可以从d:开始浏览
    但是要怎样才能从系统根目录也就是D:的上一级目录开始浏览呢?望高手们不吝赐教,感激澪涕。
      

  5.   

    java中应该有方法可以找出系统目前有多少个盘符。我用.net做过和楼主类似的功能。
      

  6.   

    luyang1016(闭月羞花猫),资源管理器用过吗?就是你一打开资源管理器时候的那个样子
      

  7.   

    java.io.File.listRoots(),获得所有根目录
      

  8.   

    谢谢,不过这个只能算勉强凑合File[] files = java.io.File.listRoots(); for (int i = 0; i < files.length; i++) {
                 System.out.println(files[i]);
    }
    会输出:
    A:\
    C:\
    D:\
    E:\
    F:\
    输出的是一个同级列表
      

  9.   

    FileSystemView.getFileSystemView().getRoots();
    这个是从桌面开始的
      

  10.   

    试过了,桌面也只是当一个普通的文件夹,不能浏览到C:,D:,E:等
    难道就没有一个路径是指向之上的吗?
      

  11.   

    不要用File.list()或 File.listFile()来得到子目录,用这个方法:
    FileSystemView.getFileSystemView().getFiles(), 这个方法可以从刚才的 getRoots()得到的桌面目录开始,得到"我的电脑", "我的文档", "网上邻居"等等, 然后递归的调用这个方法就可以从"我的电脑"得到 A: C: D: 等等.
      

  12.   

    File[] files=FileSystemView.getFileSystemView().getFiles(FileSystemView.getFileSystemView().getRoots()[0], true);

    for(int i=0;i<files.length;i++)
    {
                System.out.println(files[i]); 
    }输出:
    我的电脑
    网上邻居
    C:\Documents and Settings\dragon\My Documents
    C:\Documents and Settings\dragon\Desktop\DevNotes检查细则模板.xls
    C:\Documents and Settings\dragon\Desktop\experience.doc
    C:\Documents and Settings\dragon\Desktop\experiences.doc
    C:\Documents and Settings\dragon\Desktop\j2sdk-1_4_2_10-windows-i586-p.exe
    C:\Documents and Settings\dragon\Desktop\sources
    C:\Documents and Settings\dragon\Desktop\sources.zip
    C:\Documents and Settings\dragon\Desktop\tame.zip
    C:\Documents and Settings\dragon\Desktop\Test and JTreeTable.rar
    C:\Documents and Settings\dragon\Desktop\The Java Developers Almanac 1.4
    C:\Documents and Settings\dragon\Desktop\The Java Developers Almanac 1.4.zip
    C:\Documents and Settings\dragon\Desktop\快捷方式 到 eclipse.lnk
    C:\Documents and Settings\dragon\Desktop\瑞星卡卡上网安全助手.lnk
    C:\Documents and Settings\dragon\Desktop\金山词霸 2005.lnk
    C:\Documents and Settings\All Users\Desktop\AvRack.lnk
    C:\Documents and Settings\All Users\Desktop\DAEMON Tools.lnk
    C:\Documents and Settings\All Users\Desktop\Java Web Start.lnk
    C:\Documents and Settings\All Users\Desktop\Skype.lnk
    C:\Documents and Settings\All Users\Desktop\瑞星杀毒软件.lnk谢谢gtlang78() ,你这个我觉得已经差不多了,但是可能因为我的电脑和网上邻居不是一个直接路径,我暂时还没有搞定,再过段时间jf。
      

  13.   

    拿出来是简单,把它放到treeTable还是有点麻烦,sun java上面有个例子,是自己写一个TreeTableModel实现,但是只支持单节点,我曾依样画葫芦想让它支持多节点(例如资源管理器)不成,不会自动递归,不过javax.swing.JFileChooser显然是实现了,楼主有兴趣可以把它"挖"出来让大家分享:)
      

  14.   

    用一个递归程序做很简单啊,只要对File的方法属性了解就可以。