« Home | java.net.BindException: Address already in use: JV... » | java.lang.UnsupportedClassVersionError: » | Merge Sort » | Insertion Sort » | Selection Sort » | Program to Implement Quick Sort » | Bubble Sort » | Program for print Ramanjan Numbers » | Overriding in java » | What is overloading in java » 

Monday, June 19, 2006 

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?

thanx it was gr8 it helped me lot

Post a Comment

About me

  • I'm phanindra
  • From Hyderabad, Andhra Pradesh, India
  • An Alumini of JNTU Hyd into happening IT industry...
My profile

Visitors
Bloggeries Blog Directory BlogRankings.com singapore blog directory blog search directory
Google
 
Web conceptoftheday.blospot.com