Java FTP Rename File or Directory Example
- Details
- Written by Nam Ha Minh
- Last Updated on 20 July 2019   |   Print Email
The FTPClient class in Apache Commons Net API provides a convenient method to rename a remote file/directory on a FTP server. Syntax of this method look like:
boolean rename(String from, String to) throws IOException
Where:
- from: path/name of the file/directory which we want to rename.
- to: new name/path of the file/directory.
- The method return true if the file/directory was renamed successfully. It returns false in case the file/directory does not exist or the new name/path already exists. An IOException is thrown if there is any error occurs during communication with the server.
Here are some examples on usage of this method:
- Rename a file/directory in the current working directory (using relative path):
ftpClient.rename("photo", "photo_2012");
ftpClient.rename("Puppy.jpg", "Puppy_Old.jpg");
- Rename a file/directory using absolute path:
ftpClient.rename("/personal/photo", "/personal/photo_2012");
ftpClient.rename("/personal/photo/Puppy.jpg", "/personal/photo/Puppy_Old.jpg");
- Of course we cannot change the name in middle like this:
- Of course we cannot change the name in middle like this:
ftpClient.rename("/personal/photo/family/2012", "/personal/photo/wedding/2012");
NOTE: It’s recommended to check result of the rename() method to make sure the operation is actually successful or not. For example:
boolean success = ftpClient.rename("photo", "photo_2012"); if (success) { // rename succeeded } else { // rename failed }
The following small program demonstrates how to connect to a FTP server to rename a file and a directory, and disconnect:
package net.codejava.ftp; import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; public class FTPRenamer { public static void main(String[] args) { String server = "www.ftpserver.com"; int port = 21; String user = "username"; String pass = "password"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(user, pass); // renaming directory String oldDir = "/photo"; String newDir = "/photo_2012"; boolean success = ftpClient.rename(oldDir, newDir); if (success) { System.out.println(oldDir + " was successfully renamed to: " + newDir); } else { System.out.println("Failed to rename: " + oldDir); } // renaming file String oldFile = "/work/error.png"; String newFile = "/work/screenshot.png"; success = ftpClient.rename(oldFile, newFile); if (success) { System.out.println(oldFile + " was successfully renamed to: " + newFile); } else { System.out.println("Failed to rename: " + oldFile); } ftpClient.logout(); ftpClient.disconnect(); } catch (IOException ex) { ex.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException ex) { ex.printStackTrace(); } } } } }
Remember to update the variable server, port, userand pass according to your FTP account setting.
Related Java FTP Tutorials:
- Determine if a directory or file exists on FTP server
- Java FTP example - Get and set file modification time
- Java FTP example - Get details of a file or directory on server
- Delete a file on a FTP server
Other Java FTP Tutorials:
- Connect and login to a FTP server
- Java FTP example - Change working directory
- Java FTP create directory example
- Java FTP list files and directories example
- Java FTP file download tutorial and example
- Java FTP file upload tutorial and example
- Java FTP example - Search for files and directories
- How to upload a directory to a FTP server
- How to download a complete folder from a FTP server
Comments