Java
Chilkat2 Java
Move File to Another Directory on Server
See more FTP Examples
Moves a file from one directory to another. This is accomplished by renaming the file using a filepath that includes the directory.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Ftp2;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Ftp2 ftp = new Ftp2();
ftp.setHostname("ftp.example.com");
ftp.setUsername("login");
ftp.setPassword("password");
// Connect and login to the FTP server.
try {
ftp.connect();
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Rename the remote file (or directory)
String existingFilepath = "dirA/hello.txt";
String newFilepath = "dirB/hello.txt";
try {
ftp.renameRemoteFile(existingFilepath, newFilepath);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
ftp.disconnect();
}
}