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:

 

Other Java FTP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (FTPRemoveDirDemo.java)FTPRemoveDirDemo.java[Sample program for removing directory on FTP server]1 kB

Add comment