Program to copy a folder in java
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.lang.*;
public class Copy {
public static String copyFile(File inputFile,File outputFile){
String a="";
try{
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
}catch(Exception e){
}
return a;
}
public static void main(String[] args) throws IOException {
Copy obj = new Copy();
File inputFile = new File("your input directory which you want to replicate");
File outputFile = new File("target directory wher U want to place");
boolean success = (new File("target directory wher U want to place")).mkdir();
if (!success) {
System.out.println("Cannot create folder");
}
File dir = new File("your input directory which you want to replicate");
String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
String filename = children[i];
}
}
for(int p=0;p<children.length;p++){
File f = new File(inputFile+"/"+children[p]);
File f1 = new File(outputFile+"/"+children[p]);
System.out.println(f+"==="+outputFile);
obj.copyFile(f,f1);
}
}
}
Hi
Nice Program. If i want copy same folder in a zip archive. What all changes i want to do?
Posted by Unknown | 12:50 AM
thanx it was gr8 it helped me lot
Posted by Fakhruddin Ujjainwala | 4:01 AM