1
zj
2025-04-11 d869f01aa59927d0b4c93c908dfdfb9ca6fb326f
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package db.util;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
public class FileUtil {
    /**    
     * Member Description
     */
    private static Logger logger = LoggerFactory.getLogger(FileUtil.class); 
 
    /**    .
     * 文件字符集
     */
    private static final String CHARSET_NAME = "UTF-8";
 
    /**    
     * <p>Description: 将内容写入指定文件            </p>
     * <p>Create Time:  </p>
     * @param fileContent   内容
     * @param filePath      文件路径
     */
    public static void string2File(String fileContent, String filePath) {
        FileOutputStream fos = null;
        try {
            File outFile = new File(filePath);
            if (!outFile.exists()) {
                outFile.createNewFile();
            }
            fos = new FileOutputStream(outFile);
            fos.write(fileContent.getBytes(CHARSET_NAME), 0, fileContent.getBytes(CHARSET_NAME).length);
        } catch (Exception e) {
            logger.error("The string2File[" + filePath + "] ERROR", e);
        } finally {
            IOUtil.closeQuietly(fos);
        }
    }
 
    /**    
     * 读取文件
     * @param ins 文件流
     * @return  文件内容
     * @throws Exception 异常
     */
    public static String readStringAndClose(InputStream ins) throws Exception {
        StringBuilder buf = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
        String lineStr = null;
        while ((lineStr = br.readLine()) != null) {
            buf.append(lineStr).append("\n");
        }
        if (br != null) {
            br.close();
        }
        if (ins != null) {
            ins.close();
        }
        return buf.toString();
    }
 
    /**
     * 删除目录下的文件
     * @param path 目录名
     * @param isDeleteSelf 是否删除目录自身
     */
    public static void deleteDir(String path, boolean isDeleteSelf) {
        File dir = new File(path);
        // 检查参数
        if (dir == null || !dir.exists() || !dir.isDirectory()) {
            return;
        }
        for (File file : dir.listFiles()) {
            if (file.isFile()) {
                // 删除所有文件
                file.delete();
            }
            else if (file.isDirectory()) {
                // 递规的方式删除文件夹
                deleteDir(file.getPath(), true);
            }
        }
 
        // 删除目录本身
        if (isDeleteSelf) {
            dir.delete();
        }
    }
 
    /**    
     * 获取文件大小
     * @param filePath  文件路径
     * @return  文件大小
     * @throws FileNotFoundException 文件不存在
     */
    public static long getFileSize(String filePath) throws FileNotFoundException {
        File file = new File(filePath);
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                size = fis.available();
            } catch (Exception e) {
            } finally {
                IOUtil.closeQuietly(fis);
            }
        } else {
            throw new FileNotFoundException();
        }
        return size;
    }
 
    /**    
     * 转换文件大小格式
     * @param fileSize  文件大小
     * @param df    格式
     * @return 文件大小格式
     */
    public static String formetFileSize(long fileSize, DecimalFormat df) {
        if (df == null) {
            df = new DecimalFormat("#.000");
        }
        String result = "";
        if (fileSize < 1024) {
            result = df.format((double) fileSize) + "B";
        }
        else if (fileSize < 1048576) { // 1024*1024
            result = df.format((double) fileSize / 1024) + "K";
        }
        else if (fileSize < 1073741824) { // 1024*1024*1024
            result = df.format((double) fileSize / 1048576) + "M";
        }
        else { // 1024*1024*1024*1024
            result = df.format((double) fileSize / 1073741824) + "G";
        }
 
        return result;
    }
 
}