Home Java Javaftp Change parent directory on ftp server



Change parent directory on ftp server
Posted on: December 10, 2012 at 12:00 AM
In this section you will learn how to change the parent directory on FTP server using java.

Change parent directory on ftp server

In this section you will learn how to change the parent directory on FTP server using java.

Change Parent Directory : FTPClient class provides method to replace the parent directory.

boolean changeToParentDirectory() : Working of this method is to replace the parent directory of the present working directory. It is of boolean type. Returns true if method completed properly otherwise returns false.

It throws FTPConnectionClosedException and IOException.

Example :

This example contains code to change the current working directory using client.changeWorkingDirectory(newDir) and change the parent directory using method client.changeToParentDirectory().

Here is program -

import java.io.IOException;

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

class FtpChangeParentDirectory {
	public static void main(String[] args) throws IOException {
		FTPClient client = new FTPClient();
		boolean result;
		try {
			// Connect to the localhost
			client.connect("localhost");

			// login to ftp server
			result = client.login("admin", "admin");
			if (result == true) {
				System.out.println("Successfully logged in!");
			} else {
				System.out.println("Login Fail!");
				return;
			}
			String newDir = "/filetest";

			// Changing working directory
			result = client.changeWorkingDirectory(newDir);

			if (result == true) {
				System.out.println("Working directory is changed. Your New working directory : "
								+ newDir);
			} else {
				System.out.println("Unable to change");
			}
			result = client.changeToParentDirectory();
			if (result == true) {
				System.out.println("Parent directory is changed.");
			} else {
				System.out.println("Unable to change");
			}
		} catch (FTPConnectionClosedException e) {
			e.printStackTrace();
		} finally {
			try {
				client.disconnect();
			} catch (FTPConnectionClosedException e) {
				System.out.println(e);
			}
		}

	}
}

Output :

Successfully logged in!
Working directory is changed. Your New working directory : /filetest
Parent directory is changed.

Related Tags for Change parent directory on ftp server:


More Tutorials from this section

Ask Questions?    Discuss: Change parent directory on ftp server  

Post your Comment


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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.