2021与蓝度共同重构项目,服务端
liuhaonan
2022-11-07 ddfbc40f9ca8546a2c34865abef51630f054d5e9
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
package com.sandu.ximon.admin.utils;
 
import java.io.*;
 
/**
 * @program: machine-fast
 * @description: 文件操作类
 * @author: YSS
 * @create: 2019-04-24 13:58
 **/
public class FileUtil {
 
    /**
     * 字段写入文件
     * @param newStr        字段
     * @param filePath      文件路径
     * @return
     * @throws
     */
    public boolean writeToFile(String newStr,String filePath) throws IOException {
 
        OutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        BufferedWriter bufferedWriter = null;
 
        try{
            outputStream = new FileOutputStream(new File(filePath));
            outputStreamWriter = new OutputStreamWriter(outputStream, "utf-8");
            bufferedWriter = new BufferedWriter(outputStreamWriter);
            bufferedWriter.write(newStr);
            bufferedWriter.flush();
        }catch (Exception e){
            return false;
        }finally {
            try{
                if(bufferedWriter != null){
                    bufferedWriter.close();
                }
                if(outputStreamWriter != null){
                    outputStreamWriter.close();
                }
                if(outputStream != null){
                    outputStream.close();
                }
                return true;
            }catch (IOException e){
                return false;
            }
        }
    }
 
    public byte[] fileToByte(String filePath){
        byte[] buffer = null;
        File file = new File(filePath);
        FileInputStream fileInputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
 
 
        try {
            fileInputStream = new FileInputStream(file);
            byteArrayOutputStream = new ByteArrayOutputStream();
 
            byte[] b = new byte[1024];
 
            int n;
            while ((n = fileInputStream.read(b)) != -1){
                byteArrayOutputStream.write(b, 0, n);
            }
            buffer = byteArrayOutputStream.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(byteArrayOutputStream != null){
                    byteArrayOutputStream.close();
                }
                if(fileInputStream != null){
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
        return buffer;
    }
}