Java Move Files Between Directories

This article will show you how to use java.io.Filejava.nio.file.Files and org.apache.commons.io.FileUtils to move file or directory to another file or directory.

1. Use java.io.File.renameTo() Method.

If target path contains none exist folder, then this method will return false.

	public void renameFileUseJavaIO(String srcFilePath, String destFilePath)
	{
		if(srcFilePath!=null && srcFilePath.trim().length()>0 && destFilePath!=null && destFilePath.trim().length()>0)
		{
			/* Create source instance. */
			File srcFile = new File(srcFilePath);
			
			/* Create target instance. */
			File destFile = new File(destFilePath);
			
			/* Rename source to target. */
			boolean renameResult = srcFile.renameTo(destFile);
			
			System.out.println("Use java io to move from " + srcFilePath + " to " + destFilePath);
			
			if(renameResult)
			{
				System.out.println(" success. ");
			}else
			{
				System.out.println(" fail. ");
			}
		}
	}

2. Use java.nio.file.Files.move() Method.

If target path contains none exist folder, then this method will throw java.nio.file.NoSuchFileException.

	public void renameFileUseJavaNewIO(String srcFilePath, String destFilePath)
	{
		try
		{
			if(srcFilePath!=null && srcFilePath.trim().length()>0 && destFilePath!=null && destFilePath.trim().length()>0)
			{
				/* Create the source Path instance. */
				Path srcPathObj = Paths.get(srcFilePath); 
				
				/* Create the target Path instance. */
				Path destPathObj = Paths.get(destFilePath); 
				
				/* Rename source to target, replace it if target exist. */
				Path targetPathObj = Files.move(srcPathObj, destPathObj, StandardCopyOption.REPLACE_EXISTING);
				
				System.out.println("Use java new io to move success from " + srcFilePath + " to " + destFilePath);
			}
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

3. Use org.apache.commons.io.FileUtils.moveFile(), moveFileToDirectory() Method.

If target path contains none exist folder, you can control whether to create none exist folder or not by parameter.

Please note the class org.apache.commons.lang3.StringUtils, it provide a lot of useful methods for your convenience.

To use Apache commons io, you need add Apache commons io lib jar into your java project first. You can read Copy Directory Examples to learn how to do.

	public void renameFileUseApacheCommonsIO(String srcFilePath, String destFilePath, String targetDirectory)
	{
		try
		{
			/* Apache commons io StringUtils class provide isNoneBlank() method. */
			if(StringUtils.isNoneBlank(srcFilePath) && StringUtils.isNoneBlank(destFilePath))
			{
				/* Create source instance. */
				File srcFile = new File(srcFilePath);
				
				/* Create target instance. */
				File destFile = new File(destFilePath);
				
				/* Move to target. */
				FileUtils.moveFile(srcFile, destFile);
				
				System.out.println("Use Apache commons io to move success from " + srcFilePath + " to " + destFilePath);
				
				/* Move target to another directory. */
				File destDir = new File(targetDirectory);
				/* The third parameter is true means create target directory if not exist. */
				FileUtils.moveFileToDirectory(destFile, destDir, true);
				
				System.out.println("Use Apache commons io to move " + destFilePath + " to target directory " + targetDirectory + " successful. ");
				
			}
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

4. Use org.apache.commons.io.FileUtils.moveDirectoryToDirectory() 

This method can be used to move one folder and all it’s sub folder and files to another folder. If target folder do not exist, you can control whether create it or not use parameter.

	public void moveFilesFromDirToDir(String srcDirPath, String destDirPath)
	{
		try
		{
			/* Apache commons io StringUtils class provide isNoneBlank() method. */
			if(StringUtils.isNoneBlank(srcDirPath) && StringUtils.isNoneBlank(destDirPath))
			{
				/* Create source directory instance. */
				File srcDirFile = new File(srcDirPath);
				
				/* Create target directory instance. */
				File destDirFile = new File(destDirPath);
				
				/* Move directory to directory. */
				/* The third parameter is true means create target directory if not exist. */
				FileUtils.moveDirectoryToDirectory(srcDirFile, destDirFile, true);
				
				System.out.println("Use Apache commons io to move all files from " + srcDirPath + " to " + srcDirPath + " successful. ");
			}
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

5. Java Main Method To Invoke Above Methods.

	public static void main(String[] args) {
		
		RenameFileExample rfe = new RenameFileExample();
		
		String srcFilePath = "C:/WorkSpace/Dev2qaExample.jar";
		
		String destFilePath = "C:/WorkSpace/test/Dev2qaExample_1.jar";
		
		rfe.renameFileUseJavaIO(srcFilePath, destFilePath);
		
		rfe.renameFileUseJavaNewIO(destFilePath, srcFilePath);
		
		rfe.renameFileUseApacheCommonsIO(srcFilePath, destFilePath, "C:/WorkSpace/test1/test2");
		
		rfe.moveFilesFromDirToDir("C:/WorkSpace/test1", "C:/WorkSpace/test3");
	}

6.Example Code Download.

Download “JavaMoveFilesBetweenDirectoriesExample.zip” JavaMoveFilesBetweenDirectoriesExample.zip – Downloaded 84 times – 1 KB

(Visited 1,362 times, 6 visits today)
READ :   Java I/O(Input/Output) Overview

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.