How to detect ftp file name is a directory?

2019-05-23 06:31发布

问题:

In Java I'm trying to delete a ftp directory. But if it's not empty, I need to delete all files and sub-dirs in it, by calling files[]=ftp.dir(). I can get a list from the dir, but how can I tell if one of the items in the list is a file or sub-dir ? Is there a files[i].IsDirectory method in ftp command?

回答1:

I would use apache FTP Client for this. In that library there is a method called listFiles() that returns an array of FTPFile. For each FTPFile you can call the getType() method to determine the type code constant for a directory or a file. Quote from the java docs of getType():-

public int getType()

    Return the type of the file (one of the _TYPE constants), e.g., if it is a directory, a regular file, or a symbolic link.

    Returns:
        The type of the file.

Hope this helps!



回答2:

Take a look of this : http://www.example-code.com/java/ftp_deleteTree.asp