MrShi
4 天以前 4fabfe4dbd2eb28d07a4350597d314958cc1c281
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
150
151
152
153
154
155
156
157
package org.yzh.commons.util;
 
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.function.Function;
 
/**
 * @author yezhihao
 * https://gitee.com/yezhihao/jt808-server
 */
public class IOUtils {
 
    public static final String Separator = System.lineSeparator();
 
    public static String readIn(String classpath) {
        return readIn(classpath, StandardCharsets.UTF_8);
    }
 
    public static String readIn(String classpath, Charset charset) {
        return read(Thread.currentThread().getContextClassLoader().getResourceAsStream(classpath), charset);
    }
 
    public static String read(File file) {
        return read(file, StandardCharsets.UTF_8);
    }
 
    public static String read(File file, Charset charset) {
        try (FileInputStream is = new FileInputStream(file)) {
            byte[] bytes = new byte[is.available()];
            is.read(bytes);
            return new String(bytes, charset);
        } catch (FileNotFoundException e) {
            return null;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
    public static String read(InputStream is) {
        return read(is, StandardCharsets.UTF_8);
    }
 
    public static String read(InputStream is, Charset charset) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset))) {
            StringBuilder result = new StringBuilder(500);
            String line;
            while ((line = reader.readLine()) != null)
                result.append(line).append(Separator);
 
            int length = result.length();
            if (length == 0)
                return null;
            return result.substring(0, length - Separator.length());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
    public static void write(String source, File target) {
        write(source, StandardCharsets.UTF_8, target);
    }
 
    public static void write(String source, Charset charset, File target) {
        byte[] bytes = source.getBytes(charset);
        try (FileOutputStream os = new FileOutputStream(target)) {
            os.write(bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
    public static void write(InputStream source, File target) {
        int buffer_size = 8192;
        try (BufferedInputStream bis = new BufferedInputStream(source, buffer_size);
             FileOutputStream fos = new FileOutputStream(target)) {
 
            byte[] buffer = new byte[buffer_size];
            int length;
 
            while ((length = bis.read(buffer)) != -1) {
                fos.write(buffer, 0, length);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
    public static void copy(String sourcePath, String targetPath) {
        copy(new File(sourcePath), new File(targetPath));
    }
 
    public static void copy(File source, File target) {
        try (FileInputStream fis = new FileInputStream(source);
             FileOutputStream fos = new FileOutputStream(target);
 
             FileChannel ifc = fis.getChannel();
             FileChannel ofc = fos.getChannel()) {
 
            ByteBuffer buffer = ByteBuffer.allocate(8192);
 
            while (ifc.read(buffer) != -1) {
                buffer.flip();//切换为读模式 设置limit为position,并重置position为0
                ofc.write(buffer);
                buffer.clear();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
    public static void foreach(File file, Function<String, Boolean> function) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (Boolean.FALSE == function.apply(line))
                    break;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
    public static void delete(File file) {
        if (file.isDirectory()) {
            for (File child : file.listFiles()) {
                if (child.isDirectory()) {
                    delete(child);
                } else {
                    child.delete();
                }
            }
        }
        file.delete();
    }
 
    public static void close(AutoCloseable a) {
        if (a != null)
            try {
                a.close();
            } catch (Exception ignored) {
            }
    }
 
    public static void close(AutoCloseable a1, AutoCloseable a2) {
        close(a1);
        close(a2);
    }
 
    public static void close(AutoCloseable a1, AutoCloseable a2, AutoCloseable a3) {
        close(a1);
        close(a2);
        close(a3);
    }
}