Java Moving File and Directory to Another Directory

While developing Java application we need to do many file operation and moving file from one location to another location or moving entire directory to another directory. For doing this FileUtils class of Apache Commons IO Java library is best option as it has all checks and well tested code. Below I have written some examples to doing these operations.

Library Information

Platform information

jdk1.8.0_60, Java 8, Eclipse

Used library Name: Commons IO

Methods used: FileUtils provides four method to do file move operation. These methods have all required checks and exception handling. For any production grade Java application it is good to use this utility as it will save effort.

//Moves a directory.
static void    moveDirectory(File srcDir, File destDir)
//Moves a directory to another directory.
static void    moveDirectoryToDirectory(File src, File destDir, boolean createDestDir)
//Moves a file.
static void    moveFile(File srcFile, File destFile)
//Moves a file to a directory.
static void    moveFileToDirectory(File srcFile, File destDir, boolean createDestDir)
//Moves a file or directory to the destination directory.
static void    moveToDirectory(File src, File destDir, boolean createDestDir)

Example for moving one folder content to another folder

Moves a directory. When the destination directory is on another file system, do a “copy and delete”.

private static void moveDir(){
File srcDir = new File("C:\\example\\dir1");
File destDir = new File("C:\\example\\abc");
try {
FileUtils.moveDirectory( srcDir, destDir);
} catch (IOException e) {
e.printStackTrace();
}
}

Example for moving one file to another file

Moves a file. When the destination file is on another file system, do a “copy and delete”.

private static void moveFileExample(){
File srcFile = new File("C:\\example\\abc\\1.txt");
File destFile = new File("C:\\example\\xyz\\2.txt");
try {
FileUtils.moveFile( srcFile, destFile);
} catch (IOException e) {
e.printStackTrace();
}
}

Example for moving file to another directory

Moves a file to a directory. createDestDir If true create the destination directory, otherwise if false throw an IOException

private static void moveFileToDirExample(){
File srcFile = new File("C:\\example\\xyz\\2.txt");
File destDir = new File("C:\\example\\abc\\");
try {
FileUtils.moveFileToDirectory(srcFile, destDir, true);
} catch (IOException e) {
e.printStackTrace();
}
}

Example for moving either file or directory to another Directory

Moves a file or directory to the destination directory. createDestDir If true create the destination directory, otherwise if false throw an IOException. When the destination is on another file system, do a “copy and delete”.

private static void moveFileOrDirToDirExample(){
File srcFile = new File("C:\\example\\abc\\2.txt");
File destDir = new File("C:\\example\\xyz\\");
try {
FileUtils.moveToDirectory(srcFile, destDir, true);
} catch (IOException e) {
e.printStackTrace();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *