在 Java 中,我试图删除 ftp 目录。但如果它不为空,我需要通过调用删除其中的所有文件和子目录files[]=ftp.dir()
。我可以从目录中获取列表,但如何判断列表中的项目之一是文件还是子目录?有没有files[i].IsDirectory
ftp命令中的方法?
我会用FTP 客户端 http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPClient.html为了这。在该库中有一个方法称为列表文件() http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPClient.html#listFiles%28java.lang.String%29返回一个数组FTPFile http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPFile.html。对于每个FTPFile
你可以打电话给获取类型() http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPFile.html#getType%28%29的方法来确定类型代码常量 http://commons.apache.org/net/api/src-html/org/apache/commons/net/ftp/FTPFile.html#line.35对于目录或文件。引用 getType() 的 java 文档:-
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.
希望这可以帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)