This article shows how to move a file to another directory in the same file drive or remote server.
Files.move– Move file in local system.JSch– Move file to remove server (SFTP)
1. Move file to another directory
This Java example uses NIO Files.move to move a file from to another directory in the same local drive.
FileMove.java
package com.favtuts.io.file;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileMove {
public static void main(String[] args) {
Path source = Paths.get("/home/tvt/workspace/favtuts/newfolder/test1.txt");
Path target = Paths.get("/home/tvt/workspace/favtuts/newfolder/test2.txt");
try {
// rename or move a file to other path
// if target exists, throws FileAlreadyExistsException
Files.move(source, target);
// if target exists, replace it.
// Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
// multiple CopyOption
/*CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES,
LinkOption.NOFOLLOW_LINKS };
Files.move(source, target, options);*/
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. Move file to remote server directory
This Java example uses JSch library to move a file from the local system to another directory in a remote server, using SFTP.
P.S Assume the remote server is enabled SSH login (default port 22) using a password.
pom.xml
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
SFTPFileTransfer.java
package com.favtuts.io.howto;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class SFTPFileTransfer {
private static final String REMOTE_HOST = "1.2.3.4";
private static final String USERNAME = "";
private static final String PASSWORD = "";
private static final int REMOTE_PORT = 22;
private static final int SESSION_TIMEOUT = 10000;
private static final int CHANNEL_TIMEOUT = 5000;
public static void main(String[] args) {
// local
String localFile = "/home/favtuts/hello.sh";
// remote server
String remoteFile = "/home/favtuts/test.sh";
Session jschSession = null;
try {
JSch jsch = new JSch();
jsch.setKnownHosts("/home/favtuts/.ssh/known_hosts");
jschSession = jsch.getSession(USERNAME, REMOTE_HOST, REMOTE_PORT);
// authenticate using private key
// jsch.addIdentity("/home/favtuts/.ssh/id_rsa");
// authenticate using password
jschSession.setPassword(PASSWORD);
// 10 seconds session timeout
jschSession.connect(SESSION_TIMEOUT);
Channel sftp = jschSession.openChannel("sftp");
// 5 seconds timeout
sftp.connect(CHANNEL_TIMEOUT);
ChannelSftp channelSftp = (ChannelSftp) sftp;
// transfer file from local to remote server
channelSftp.put(localFile, remoteFile);
// download file from remote server to local
// channelSftp.get(remoteFile, localFile);
channelSftp.exit();
} catch (JSchException | SftpException e) {
e.printStackTrace();
} finally {
if (jschSession != null) {
jschSession.disconnect();
}
}
}
}
Please visit this file Transfer using SFTP in Java (JSch).
Note
The NIO
Files.movecan’t move a file from the local system to a remote server directory.
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-io