This copy util seperates the selected files from given sub folders.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class CopyUtil {
public static boolean removeDirectory(File directory) {
// Remove the directory
if (directory == null)
return false;
if (!directory.exists())
return true;
if (!directory.isDirectory())
return false;
String[] list = directory.list();
if (list != null) {
for (int i = 0; i < list.length; i++) {
File entry = new File(directory, list[i]);
if (entry.isDirectory()) {
if (!removeDirectory(entry))
return false;
} else {
if (!entry.delete())
return false;
}
}
}
return directory.delete();
}
public static boolean copy(String source, String target) {
// Copy the contents of source file to target file
try {
BufferedReader br = new BufferedReader(new FileReader(new File(
source)));
PrintWriter pw = new PrintWriter(new File(target));
String eachLine = null;
while ((eachLine = br.readLine()) != null)
pw.println(eachLine);
pw.close();
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args) {
String input = "";
if (args.length == 0)
System.out
.println("Please give input folder path through command line..\nAlso Please make sure that it has a file name input.csv which contains the list of files to be copied to target location");
else {
input = args[0].trim();
input = input.replace("\\", "/");
File inputFolder = new File(input);
if (!inputFolder.isDirectory())
System.out.println("Directory doesn't exist :: " + inputFolder);
else {
boolean success = false;
if ((new File(inputFolder + "/target")).isDirectory()) {
if (removeDirectory(new File(inputFolder + "/target")))
System.out
.println("Deleting Existing Target Directory :: "
+ inputFolder + "/target");
}
String listOfSubs[] = inputFolder.list();
if ((new File(inputFolder + "/target")).mkdir())
System.out.println("Empty Target Directory Created .. "
+ inputFolder + "/target");
for (int i = 0; i < listOfSubs.length; i++) {
if (new File(inputFolder + "/" + listOfSubs[i])
.isDirectory()) {
success = (new File(inputFolder + "/target/"
+ listOfSubs[i])).mkdir();
if (success)
System.out.println("SubDirectory Created :: "
+ inputFolder + "/target/" + listOfSubs[i]);
else
System.out
.println("Failed to Create SubDirectory :: "
+ inputFolder
+ "/target/"
+ listOfSubs[i]);
}
}
File inputcsv = new File(inputFolder + "/input.csv");
BufferedReader br;
try {
br = new BufferedReader(new FileReader(inputcsv));
StringTokenizer st = null;
String eachLine = "";
String sourceFile = "";
String targetFile = "";
String foldername = "";
String filename = "";
while ((eachLine = br.readLine()) != null) {
st = new StringTokenizer(eachLine, ",");
foldername = st.nextToken().trim();
filename = st.nextToken().trim();
sourceFile = inputFolder + "/" + foldername + "/"
+ filename;
sourceFile = sourceFile.trim();
targetFile = inputFolder + "/target/" + foldername
+ "/" + filename;
targetFile = targetFile.trim();
if (copy(sourceFile, targetFile))
System.out.println("Copied :: " + sourceFile
+ " -- " + targetFile);
else
System.out.println("Copy Failed :: " + sourceFile
+ " -- " + targetFile);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}