mac
2023-09-28 2c7615cd73dfa6a7ca4df975430d2217524513d2
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package com.hdl.photovoltaic.other;
 
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
 
import com.hdl.photovoltaic.HDLApp;
import com.hdl.photovoltaic.config.UserConfigManage;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
/**
 * 文件操作的逻辑
 */
public class HdlFileLogic {
 
    private static volatile HdlFileLogic sHdlFileLogic;
 
    /**
     * 获取当前对象
     *
     * @return HdlFileLogic
     */
    public static synchronized HdlFileLogic getInstance() {
        if (sHdlFileLogic == null) {
            synchronized (HdlFileLogic.class) {
                if (sHdlFileLogic == null) {
                    sHdlFileLogic = new HdlFileLogic();
                }
            }
 
        }
        return sHdlFileLogic;
    }
 
    private static final String TAG = "FileUtils";
 
    private String getUserId() {
        return UserConfigManage.getInstance().getUserId();
    }
 
    private String getHomeId() {
        return UserConfigManage.getInstance().getHomeId();
    }
    //region    ---------root路径-----------
 
    /**
     * 获取手机内部存储文件路径
     */
    private String getAPPInternalStoreFilesPath() {
        return HDLApp.getInstance().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getPath();
    }
 
    /**
     * 获取存放文件【用户】根路径
     */
    public String getCurrentUserRootPath() {
        return getAPPInternalStoreFilesPath() + "/" + getUserId();
    }
 
    /**
     * 获取存放文件【住宅】根路径
     */
    public String getCurrentHomeRootPath() {
        return getAPPInternalStoreFilesPath() + "/" + getUserId() + "/home";
    }
 
    /**
     * 住宅文件名称
     *
     * @return -
     */
    public String getCurrentHomeFileName() {
        return "/" + getHomeId() + ".json";
    }
 
 
    /**
     * 获取当前住宅文件全路径
     */
    public String getCurrentHomeFilesFullPath() {
        return getCurrentHomeRootPath() + getCurrentHomeFileName();
    }
 
    //endregion
 
    //region    ---------【文件夹】操作-----------
 
    /**
     * 预创建文件夹
     */
    public void createDirectory() {
        //存放用户信息
        this.createFileDir(this.getCurrentUserRootPath());
        //存放住宅信息
        this.createFileDir(this.getCurrentHomeRootPath());
    }
 
    /**
     * 创建文件夹
     *
     * @param fullPath fullPath 全路径
     * @return -
     */
    public boolean createFileDir(String fullPath) {
        try {
            File file = new File(fullPath);
            if (!file.isDirectory()) {
                boolean succeed = file.mkdirs();
                HdlLogLogic.print("创建文件夹==" + succeed);
                return succeed;
            }
            return true;
        } catch (Exception e) {
            HdlLogLogic.print("创建文件夹有异常==" + e.getMessage());
            return false;
        }
    }
 
 
    /**
     * 删除文件夹
     *
     * @param fullPath 全路径
     */
    public void deleteDirectory(String fullPath) {
        try {
            File file = new File(fullPath);
            if (file.isDirectory()) {
                boolean succeed = file.delete();
                HdlLogLogic.print("删除文件夹==" + succeed);
            }
        } catch (Exception e) {
            HdlLogLogic.print("删除文件夹有异常==" + e.getMessage());
        }
 
    }
    //endregion
 
    //region    ---------【文件】操作-----------
 
    /**
     * 创建文件
     *
     * @param fullPath 全路径
     * @return -
     */
    public boolean createFile(String fullPath) {
        try {
            File file = new File(fullPath);
            if (!file.exists()) {
                boolean succeed = file.createNewFile();
                HdlLogLogic.print("创建文件==" + succeed);
                return succeed;
            }
            return true;
        } catch (Exception e) {
            HdlLogLogic.print("创建文件有异常==" + e.getMessage());
            return false;
        }
    }
 
    /**
     * 删除文件
     *
     * @param fullPath 全路径
     * @return -
     */
    public boolean deleteFile(String fullPath) {
        try {
            File file = new File(fullPath);
            if (file.exists()) {
                boolean succeed = file.delete();
                HdlLogLogic.print("删除文件==" + succeed);
                return succeed;
            }
            return true;
        } catch (Exception e) {
            HdlLogLogic.print("删除文件有异常==" + e.getMessage());
            return false;
        }
 
    }
 
    /**
     * 写入文件
     *
     * @param fullPath 全路径
     * @param data     数据
     */
    public void writeFile(String fullPath, String data) {
        try {
            File file = new File(fullPath);
            if (!file.exists()) {
                if (!createFile(fullPath)) {
                    //创建失败直接返回
                    return;
                }
            }
            FileOutputStream d = new FileOutputStream(file);
            d.write(data.getBytes());
            d.flush();
            d.close();
            HdlLogLogic.print("写入文件成功==" + fullPath);
        } catch (Exception e) {
            HdlLogLogic.print("写入文件有异常==" + e.getMessage());
        }
 
    }
 
    /**
     * 读取文件
     *
     * @param filepath 全路径
     * @return 数据
     */
    public String readFile(String filepath) {
        try {
            String fileContent = "";
            if (null == filepath) {
                Log.d(TAG, "Error: Invalid file name!");
                return fileContent;
            }
 
            File f = new File(filepath);
            if (!f.exists()) {
                return fileContent;
            }
            FileInputStream fis = new FileInputStream(f);
            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);
            fis.close();
            HdlLogLogic.print("读取文件成功==" + filepath);
            fileContent = new String(bytes);
            return fileContent;
        } catch (Exception e1) {
            HdlLogLogic.print("读取文件有异常==" + e1.getMessage());
            return "";
        }
 
    }
    //endregion
 
    /**
     * 把位图数据保存到指定路径的图片文件
     */
    public void writeImage(String path, Bitmap bitmap) {
        try {
            //根据指定文件路径构建缓存输出流对象
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
            //把位图数据压缩到缓存输出流中
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
            //完成缓存输出流的写入动作
            bos.flush();
            //关闭缓存输出流
            bos.close();
 
 
        } catch (Exception e) {
        }
    }
    //</editor-fold>
 
    /**
     * 从指定路径的图片文件中读取位图数据
     */
    public Bitmap readImage(String path) {
        try {
            //根据指定文件路径构建缓存输入流对象
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
            //从缓存输入流中解码位图数据
            Bitmap bitmap = BitmapFactory.decodeStream(bis);
            //关闭缓存输入流
            bis.close();
            return bitmap;
        } catch (Exception e) {
        }
        return null;
    }
 
 
}