File Copy From Local Folder or Network Folder Using Java With Capability To Limit The Number

One of my network folders had more than 20000 files and every time I tried to open the foder by myself to copy some files, the windows would simply freeze or slow down to an extent that I could no longer operate on that folder. To get rid of the frustration and to be able to copy some of the files from that network drive to my local machine (I needed the files to do performance test on the attachments), I wrote this simple utility.

You can use this utility to copy files from one location to another.

  1. The source location can be over the network or another folder on your local machine.
  2. The destination fodler can also be another network location or simply another folder on your local machine.

You can also specify how many files you want to copy from the source location. For this you need to set the integer ‘numberOfFilesToCopy’ to the desired value.

Here is the code for the program.


 * @author Kushal Paudyal
 * Last Modified on: 2011-11-03
 * Utility to copy files from one folder to another.
 * - Can copy from Network location also.
 * - Can limit the total number of files to copy

public class FileCopy {

	public static void main(String[] args) throws IOException {
		String fileLocationSourceNetwork = "\\mynetwork\mydata\";
		String fileLocationSourceDrive = "C:\mavenRepo";

		String fileLocationDestination = "C:\temp\";
		int numberOfFilesToCopy = 2;

		/**Copy the files from the network**/
		copyFiles(	fileLocationSourceNetwork,


		/**Copy the files from the drive**/
		copyFiles(	fileLocationSourceDrive,


	public static void copyFiles(String fileLocationSource,
			String fileLocationDestination, int numberOfFilesToCopy) {
		File inputLocation = new File(fileLocationSource);
		if (inputLocation.isDirectory()) {
			System.out.println("Listing the files...");
			File[] attachmentFiles = inputLocation.listFiles();
			System.out.println("Total files in the folder: "
					+ attachmentFiles.length);
			for (File aFile : attachmentFiles) {
				if (!aFile.isDirectory()) {
					String fileName = aFile.getName();
					String sourceFileName = aFile.getAbsolutePath();
					String destinationFileName = fileLocationDestination
							+ fileName;
					copyFile(sourceFileName, destinationFileName);
				if (numberOfFilesToCopy >= 0) {
					if (--numberOfFilesToCopy == 0) {

	 * @param sourceFileName
	 * @param destionFileName
	 *            Copies a single file from source location to a destination
	 *            location.
	private static void copyFile(String sourceFileName, String destionFileName) {
		try {
			System.out.println("Reading..." + sourceFileName);
			File sourceFile = new File(sourceFileName);
			File destinationFile = new File(destionFileName);
			InputStream in = new FileInputStream(sourceFile);
			OutputStream out = new FileOutputStream(destinationFile);

			byte[] buffer = new byte[1024];
			int length;
			while ((length = > 0) {
				out.write(buffer, 0, length);
			System.out.println("Copied: " + sourceFileName);
		} catch (Exception ex) {

The following is the output of this program:

Listing the files...
Total files in the folder: 23632
Copied: \mynetworkmydatainput_1045863400149541657_010106687.txt
Copied: \mynetworkmydatainput_1045863400149541657_010107578.txt

Listing the files...
Total files in the folder: 16
Copied: C:mavenRepodb2jcc.jar

  1. How to read / write UTF8 and Non-UTF8 files in Java
  2. How To Print A Text File In Java
  3. How to read a tab separated or tab delimited file in Java program and print the content to console
  4. Computing the total, free and usable disk space easily using JDK 1.6
  5. Reading the Resource or Property files in Java as a file stream and outputting the content to console
  6. File Copy From Local Folder or Network Folder Using Java With Capability To Limit The Number
  7. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  8. Reading / Writing File in Java and String Manipulation
  9. How To Read DOC file Using Java and Apache POI
  10. Writing To Excel File Using Apache POI
  11. Reading Excel File Using Java And Apache POI
  12. Calculating Folder Size In Java
  13. How To Rename A File In Java
  14. Calculating Folder Size Graphically and Generating Directory Size Chart In Java
Tagged , , , , , , , . Bookmark the permalink.

4 Responses to Calculating Folder Size Graphically and Generating Directory Size Chart In Java

  1. nocdib says:

    Thank you for this! It is just what I need

  2. d says:

    would that help you tocreate the file from ubunt server

  3. komal gaikwad says:

    in this code i got problem of array out of bound plz tell me solution.

  4. komal gaikwad says:

    In this code i got error
    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1
    plz send me proper code….

Leave a Reply

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