Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- class akik
- {
- public static void main(String s[])
- {
- try
- {
- BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Enter the path of the Source directory");
- String n1=bfr.readLine();
- File sour = new File(n1);
- System.out.println("Enter the path of the modified directory");
- String n2=bfr.readLine();
- File mod = new File(n2);
- System.out.println("Enter the path of the saved directory");
- String n3=bfr.readLine();
- File save = new File(n3);
- (new File(n3+"/unique")).mkdir();
- File saveu = new File(n3+"/unique");
- String[] chils = sour.list();
- if (chils == null) {
- System.out.println("Invalid Directory");
- }
- String[] chilm =mod.list();
- if (chilm == null) {
- System.out.println("Invalid Directory");
- }
- for(int i=0;i<chilm.length;i++)
- {
- Boolean ff=false;
- for(int j=0;j<chils.length&&!ff;j++)
- {
- if(chilm[i].equals(chils[j])&&((new File(n2,chilm[i])).isDirectory())&&(new File(n1,chils[j]).isDirectory()))
- {
- check(n2+"/"+chilm[i],n1+"/"+chils[j],n3);
- ff=true;
- }
- else if(chilm[i].equals(chils[j]))
- {
- ff=true;
- if(!compare(n2+"/"+chilm[i],n1+"/"+chils[j]))
- {
- System.out.println();
- copy(new File(n2+"/"+chilm[i]),new File(save,chilm[i]));
- }
- }
- }
- if(!ff)
- {
- copy(new File(n2+"/"+chilm[i]),new File(saveu,chilm[i]));
- }
- }
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- System.out.println("FILE COPY PROGRAM DONE");
- }
- static void check(String a,String b,String c)
- {
- try
- {
- File f1=new File(a);
- File f2=new File(b);
- File save=new File(c);
- File saveu=new File(c+"/unique");
- String[] s1=f1.list();
- String[] s2=f2.list();
- for(int i=0;i<s1.length;i++)
- {
- Boolean ff=false;
- for(int j=0;j<s2.length&&!ff;j++)
- {
- if(s1[i].equals(s2[j]))
- {
- if((new File(a,s1[i]).isDirectory())&&(new File(b,s2[j]).isDirectory()))
- {
- check(a+"/"+s1[i],b+"/"+s2[j],c);
- ff=true;
- }
- else if(!compare(a+"/"+s1[i],b+"/"+s2[j]))
- {
- copy(new File(f1,s1[i]),new File(c,s1[i]));
- ff=true;
- }
- }
- }
- if(!ff)
- {
- copy(new File(f1,s1[i]),new File(c+"/unique",s1[i]));
- }
- }
- }
- catch(Exception e)
- {
- System.out.println(e+"akik");
- }
- }
- static Boolean compare(String filePath1,String filePath2){
- try{
- File f1 = new File(filePath1);
- File f2 = new File(filePath2);
- if(f1.length() == f2.length()){
- FileInputStream fis1 = new FileInputStream(f1);
- FileInputStream fis2 = new FileInputStream(f2);
- while(true){
- int a = fis1.read();
- int b = fis2.read();
- if(a != b){
- return false;
- }
- if(a == -1){
- return true;
- }
- }
- }else{
- return false;
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- return false;
- }
- static void copy(File src, File dst) throws IOException {
- InputStream in = new FileInputStream(src);
- OutputStream out = new FileOutputStream(dst);
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) > 0) {
- out.write(buf, 0, len);
- }
- in.close();
- out.close();
- }
- }
Add Comment
Please, Sign In to add comment