Java FTP Remove empty directory example
- Details
- Written by Nam Ha Minh
- Last Updated on 20 July 2019   |   Print Email
To remove an empty directory on a FTP server using Apache Commons Net API, we can use method removeDirectory() of FTPClient class. Signature of this method is as follows:
public boolean removeDirectory(String pathname) throws IOException
The method will issue a FTP command RMD to the FTP server to remove the remote directory specified by pathname. It returns true if the remote directory is successfully removed, or false otherwise (i.e the directory does not exist, is not empty or is a file).
The method throws an FTPConnectionClosedException exception if connection with the server is already closed, or an IOException exception if there is an I/O error occurs during communication with the server.
Here is an usage example for the method removeDirectory():
FTPClient ftpClient = new FTPClient(); // code to connect and login... String dirToRemove = "/Path/Of/Dir/To/Remove"; try { boolean deleted = ftpClient.removeDirectory(dirToRemove); if (deleted) { System.out.println("The directory was removed successfully."); } else { System.out.println("Could not delete the directory, it may not be empty"); } } catch (IOException ex) { System.out.println("Oh no, there was an error: " + ex.getMessage()); } // code to log out and disconnect...
To illustrate usage of the removeDirectory() method, we provide a working example program which:
- Logins to a FTP server.
- Removes a directory on the server. Note that the directory must be empty.
- Logs out and disconnects.
Here is the source code:
import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; public class FTPRemoveDirDemo { public static void main(String[] args) { String server = "www.myserver.com"; int port = 21; String user = "user"; String pass = "pass"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { System.out.println("Connect failed"); return; } boolean success = ftpClient.login(user, pass); if (!success) { System.out.println("Could not login to the server"); return; } String dirToRemove = "/public/video/temp"; boolean deleted = ftpClient.removeDirectory(dirToRemove); if (deleted) { System.out.println("The directory was removed successfully."); } else { System.out.println("Could not delete the directory, it may not be empty."); } } catch (IOException ex) { System.out.println("Oh no, there was an error: " + ex.getMessage()); ex.printStackTrace(); } finally { // logs out and disconnects from server try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } } }
NOTE: To use Apache Commons Net API, you must have commons-net-VERSION.jar file available in the classpath. You can download latest distribution of Apache Commons Net API at: http://commons.apache.org/net/download_net.cgi
Related Java FTP Tutorials:
- Java FTP create directory example
- Java FTP list files and directories example
- List files and directories recursively on a FTP server
- Java FTP example - Search for files and directories
- Creating nested directory structure on a FTP server
- Java FTP example - Calculate total sub directories, files and size of a directory
- Determine if a directory or file exists on FTP server
Other Java FTP Tutorials:
- Connect and login to a FTP server
- Java FTP file download tutorial and example
- Java FTP file upload tutorial and example
- Java FTP delete file example
- How to upload a directory to a FTP server
- How to download a complete folder from a FTP server
Comments