Monday, March 10, 2014

Java program to copy one directory filres to another directory

import java.io.*;
class dirfile{

      public static void main(String arg[])throws IOException{
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("entre the location ");
  String s1=br.readLine();
  System.out.println("entre the  new location ");
  String s2=br.readLine();
File f1=new File(s1);
   File f2=new File(s2);
   copy(f1,f2);

  }
public static void copy(File f1,File f2)throws IOException{
                if(f1.isDirectory()){
    if(!f2.exists()){
                       f2.mkdir();
}
                       String f[]=f1.list();
                            for(int k=0;k<f.length;k++){
             File f3=new File(f1,f[k]);
             File f4=new File(f2,f[k]);
             copy(f3,f4);
 }
 }
 else{
  FileInputStream fis=new FileInputStream(f1);
  FileOutputStream fos=new FileOutputStream(f2);
                  PrintStream fs=new PrintStream(fos);
int ch;
                byte []b=new byte[1024];
                while((ch=fis.read(b))!=-1){
                  fs.write(b,0,ch);
                   }
  }
}
}


OUTPUT



1 comment:

Anonymous said...

Spelling of enter is wrong! :'(