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");

client.connect("localhost");
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 {
			client.connect("localhost");
			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) {
			e.printStackTrace();
		} finally {
			try {
				client.disconnect();
			} catch (FTPConnectionClosedException e) {
				System.out.println(e);
			}
		}
	}
}

Output :

Successfully logged in!
The directory was removed successfully.

Advertisements

Ads
Share on Google+Share on Google+

FTP Server : Remove Directory

Posted on: December 7, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: FTP Server : Remove Directory  

Post your Comment


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

Ads

 

Ads