Sunshine
2024-11-04 919ed870ea1def0cfdd1dff23bec204975e7f34c
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
package com.nova.sankuai.domain.api.yinsheng;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
/**
 * @类名称:TextFileHelper
 * @类描述:读取文件
 * @作者:zhanggy
 * @日期:2021年8月11日
 */
public final class TextFileHelper {
 
    private TextFileHelper() {
    }
 
    /**
     * 读文件
     * @param file 文件路径
     * @return 文件内容
     */
    public static byte[] readFile(String file) throws IOException {
        int offset = 0;
        try (FileInputStream in = new FileInputStream(file)) {
            byte[] out = new byte[in.available()];
 
            if (out.length < offset + in.available()) {
                throw new IOException("Illegal Argument: filepath");
            }
 
            byte[] buffer = new byte[1024];
            int numRead;
            while ((numRead = in.read(buffer, 0, buffer.length)) >= 0) {
                System.arraycopy(buffer, 0, out, offset, numRead);
                offset += numRead;
            }
            return out;
        }
    }
 
    /**
     * 写文件
     */
    public static void writeFile(String filePath, byte[] data) throws IOException {
        try (FileOutputStream out = new FileOutputStream(filePath)) {
            out.write(data, 0, data.length);
        }
    }
 
}