935090232@qq.com
2022-02-17 94f76200c0ac4d87e73afa4895f6088bfb02b6b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
/**
 * 文件对比复制
 * @author JIANGYOUYAO
 * @date 2021/6/7 0007
 */
public class filecopy {
 
 
    static  String targetFilePath="/mnt/sdc/webresource";
 
    static  String sourceFilePaht="C:\\Users\\Administrator\\Desktop\\webresource";
 
    public static void main(String[] args) throws IOException {
 
 
        File sourceFile=new File(sourceFilePaht);
        traverseFolder(sourceFile);
 
 
    }
 
 
    public static  void traverseFolder(File file) throws IOException {
 
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null == files || files.length == 0) {
                System.out.println("文件夹是空的!");
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        //对比target是否存在
                        final String s = file2.getCanonicalPath().replaceAll("webresourceback", "webresource");
                        File f=new File(s);
                        if(!f.exists()){
                            System.out.println("复制文件:" + file2.getAbsolutePath());
                            f.mkdir();
                        }else{
                            System.out.println("文件:" + file2.getAbsolutePath()+"存在");
                        }
                        traverseFolder(file2);
                    } else {
 
                        //对比target是否存在,不存在则copy
                        final String s = file2.getCanonicalPath().replaceAll("webresourceback", "webresource");
                        File f=new File(s);
                        if(!f.exists()){
                            System.out.println("复制文件:" + file2.getAbsolutePath());
                            FileInputStream in=new FileInputStream(file2);
                            FileOutputStream out=new FileOutputStream(f);
                            byte[] buff=new byte[1024];
                            int length=in.read(buff);
                            while (length>0){
                                out.write(buff,0,length);
                                length=in.read(buff);
                            }
                            out.close();
                            in.close();
 
                        }else{
                            System.out.println("文件:" + file2.getAbsolutePath()+"存在");
                        }
 
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }
    }
 
 
 
}