Quantcast
Channel: Kushal's Java Blog | Software Engineering Blog
Viewing all articles
Browse latest Browse all 25

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

$
0
0

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.


package com.kushal.tools;

/**
 * @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
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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,
					fileLocationDestination,
					numberOfFilesToCopy);

		System.out.println("\n\n");

		/**Copy the files from the drive**/
		copyFiles(	fileLocationSourceDrive,
				fileLocationDestination,
				numberOfFilesToCopy);

	}

	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) {
						break;
					}
				}
			}
		}
		System.out.println("Completed...");
	}

	/**
	 *
	 * @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 = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}
			in.close();
			out.close();
			System.out.println("Copied: " + sourceFileName);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

The following is the output of this program:

Listing the files...
Total files in the folder: 23632
Reading...\\mynetwork\mydata\input_1045863400149541657_010106687.txt
Copied: \\mynetwork\mydata\input_1045863400149541657_010106687.txt
Reading...\\mynetwork\mydata\input_1045863400149541657_010107578.txt
Copied: \\mynetwork\mydata\input_1045863400149541657_010107578.txt
Completed...



Listing the files...
Total files in the folder: 16
Reading...C:\mavenRepo\com.ibm.mqjms.jar
Copied: C:\mavenRepo\com.ibm.mqjms.jar
Reading...C:\mavenRepo\db2jcc.jar
Copied: C:\mavenRepo\db2jcc.jar
Completed...

Originally posted 2012-02-04 14:59:11.

Share


Viewing all articles
Browse latest Browse all 25

Trending Articles