How to Copy Files from One Folder to Another in Java

The following Java program copies files with .java extension from one folder to another folder. You can easily change the extension .java to anything you want. For example, you might want to copy just .txt files from one directory to another.

This program takes two input parameters. The first one is the source directory and the second one is the target directory. Please ensure that you enter full path of the directories. This program also displays errors encountered if any during the file copying.

Note that this program doesn't delete the source file. This program only uses the File and File stream classes.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.OutputStream;

/**
* Copy all the files in a folder with specific extension to another folder. 
* Both the folders are specified on the command line. 
* The program currently uses .java extension as filter.
**/
public class CopyFilesWithExtension {
	
	public static void main(String[] args) {
		if(args.length!=2) {
			System.out.println("Command usage: java CopyFilesWithExtension sourcedirectory targetdirectory");
		}
		
		String sourceFolder = args[0];
		String targetFolder = args[1];
		
		File sFile = new File(sourceFolder);
		// Find files with specified extension
		File[] sourceFiles = sFile.listFiles(new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String name) {
				if(name.endsWith(".java")) {// change this to your extension
					return true;
				}else {
					return false;
				}
			}
		});
		
		// let us copy each file to the target folder
		for(File fSource:sourceFiles) {
			File fTarget = new File(new File(targetFolder), fSource.getName());
			copyFileUsingStream(fSource,fTarget);
			// fSource.delete(); // Uncomment this line if you want source file deleted
		}
		
		
	}
	
	/**
	 * Copies a file using the File streams
	 * @param source
	 * @param dest
	 */
	private static void copyFileUsingStream(File source, File dest)  {
	    InputStream is = null;
	    OutputStream os = null;
	    try {
	        is = new FileInputStream(source);
	        os = new FileOutputStream(dest);
	        byte[] buffer = new byte[1024];
	        int length;
	        while ((length = is.read(buffer)) > 0) {
	            os.write(buffer, 0, length);
	        }
	    }catch(Exception ex) {
	    	System.out.println("Unable to copy file:"+ex.getMessage());
	    }	
	    finally {
	    	try {
	    		is.close();
	    		os.close();
	    	}catch(Exception ex) {}
	    }
	}

}