shichongfu
2023-02-17 8d54cbb1bbb8e31978d979833aeb0dd1b2848d4e
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.lx.product.seal.utils;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Map;
 
import com.lx.product.seal.log.Logger;
 
public class FileUtil {
    private static final String row = "\n";
    public static Logger log = Logger.getInstance();
 
    /**
     * 复制文件
     * @param sourceFile
     * @param targetFile
     */
    public static void copyFile(File sourceFile, File targetFile) {
 
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        try {
            if (!targetFile.getParentFile().exists()) {
                targetFile.getParentFile().mkdirs();
            }
            // 新建文件输入流并对它进行缓冲
            inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
            // 新建文件输出流并对它进行缓冲
            outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
            // 缓冲数组
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            // 刷新此缓冲的输出流
            outBuff.flush();
        } catch (Exception e) {
            log.writeInfo(e.getMessage(), Logger.ERROR_TYPE);
        } finally {
            // 关闭流
            if (inBuff != null) {
                try {
                    inBuff.close();
                } catch (Exception ee) {
                    log.writeInfo(ee.getMessage(), Logger.ERROR_TYPE);
                }
            }
            if (outBuff != null) {
                try {
                    outBuff.close();
                } catch (Exception ee) {
                    log.writeInfo(ee.getMessage(), Logger.ERROR_TYPE);
                }
            }
        }
    }
 
    /**
     * 修改文件
     * @param sourceFile
     * @param targetFile
     */
    public static void modifyFile(File sourceFile, File targetFile, Map<String, String> items, String readEncode, String writeEncode) {
        BufferedWriter bw = null;
        try (InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(sourceFile), readEncode);
             BufferedReader br = new BufferedReader(inputStreamReader);) {
            StringBuilder content = new StringBuilder(512);
            String line = null;
            while ((line = br.readLine()) != null) {
                if (line.startsWith("pause")) {
                    continue;
                }
                for (Map.Entry<String, String> entry : items.entrySet()) {
                    if (line.trim().startsWith(entry.getKey())) {
                        line = entry.getValue();
                    }
                }
                content.append(line).append("\r\n");
            }
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), writeEncode));
            bw.write(content.toString());
            // 刷新此缓冲的输出流
            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
        }
    }
 
    public static void modifyFile(File sourceFile, File targetFile, Map<String, String> items, String encode) {
        modifyFile(sourceFile, targetFile, items, encode, encode);
    }
 
    public static void modifyFile(File sourceFile, File targetFile, Map<String, String> items) {
        modifyFile(sourceFile, targetFile, items, "utf-8");
    }
}