我用 sun.net.ftp.* 这个包中的 list() 函数显示出了ftp服务器中的文件目录,但是怎样区分哪些东西是文件,哪些东西又是目录呢?我的方法如下:            jList list=new jList();
            DefaultListModel model = new DefaultListModel();
            in = new BufferedReader(new InputStreamReader(ftp.list()));
            String line = null;
            while ((line = in.readLine()) != null) {
                model.addElement(line.substring(0));
            }
            list.setModel(model);

解决方案 »

  1.   

    用instanceof判断属于那个类,就可以区分。
      

  2.   

    java.io Class FileisDirectory
    public boolean isDirectory()
    Tests whether the file denoted by this abstract pathname is a directory. Returns:
    true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise isFile
    public boolean isFile()
    Tests whether the file denoted by this abstract pathname is a normal file. A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file. Returns:
    true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise 
      

  3.   

    如果对方是linux服务器,ftp.list()会返回文件的性质,类似:
    -rw-rw-r--    1 keiy      other            14 Dec  2  2003 1
    -rw-rw-r--    1 keiy      other            54 Dec  2  2003 t.c
    -rw-rw-r--    1 keiy      other            69 Dec  2  2003 t1
    -rw-rw-r--    1 keiy      other            69 Dec  2  2003 t2
    drwxrwxr-x    2 keiy      other          4096 Aug  6 18:05 ttt
    只要判行首的第一个字母就可以了(d为目录)
    ----------------------------------------------
    我从不用windwos的ftp,所以windwos你可看一下,估计有类似返回(或windows目录格式,也可用<DIR>来判)
      

  4.   

    interhanchi(Stallman'fans)已经说的很清楚了,就用这几个方法
      

  5.   

    keiy的是对的,判断首字母, if(line.startsWith("d")) {...}具体list()也就是FTP的ls或者dir命令的返回的格式,参考
    http://www.novell.com/documentation//nw5/docui/index.html#../usprint/unixpenu/data/hpyvrshh.html^h1ef7fx1