Home Java Javaftp FTP Server : Remove Directory


Share on Google+Share on Google+

FTP Server : Remove Directory

In this section you will learn how to delete directory on FTP server using java.

Remove Directory on FTP Server

In this section you will learn how to delete directory on FTP server using java.

Remove Directory :

By using FTP Server we can transfer files from one computer to another computer through network and internet. You must be valid user for using FTP server. Before applying any operation on FTP server first we need to connect to the server by using connect() method of FTPClient class, next input valid user name and password to login by calling method  login("username","password");

result = client.login("admin", "admin");

To delete directory on ftp server FTPClient class provides removeDirectory() method. Directory is removed only if it is empty.

boolean removeDirectory(String name) : It returns boolean value. True value represents that the directory is removed successfully. It returns false if this method fail to delete the specified directory.

Example : In this example we are deleting directory "/ftpNew" by using removeDirectory() of FTPClient class.

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;

class FtpDeleteDir {
	public static void main(String[] args) throws IOException {
		FTPClient client = new FTPClient();
		boolean result;
		try {
			result = client.login("admin", "admin");

			if (result == true) {
				System.out.println("Successfully logged in!");
			} else {
				System.out.println("Login Fail!");
			String dirToRemove = "/ftpNew";

			boolean deleted = client.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 (FTPConnectionClosedException e) {
		} finally {
			try {
			} catch (FTPConnectionClosedException e) {

Output :

Successfully logged in!
The directory was removed successfully.

If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 7, 2012

Ask Questions?    Discuss: FTP Server : Remove Directory  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image