wjc
2023-06-07 07ccc78892b26dabc976363c874c177230f9bbcb
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
package com.hdl.photovoltaic.utils;
 
import android.content.Context;
import android.util.Log;
 
import com.hdl.photovoltaic.other.HdlLogLogic;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
 
 
public class FileUtils {
 
    private static final String TAG = "FileUtils";
 
    /**
     * 获取内部存储文件路径
     *
     * @param context 上下文
     * @return -
     */
    public static String getInternalStoreFilesPath(Context context) {
        if (context == null) {
            return "";
        }
        return context.getFilesDir().getAbsolutePath();
    }
 
    public static File createFile(String dirPath, String fileName) {
        try {
            File dirFile = new File(dirPath);
            if (!dirFile.exists()) {
                if (!createFileDir(dirFile)) {
                    HdlLogLogic.print(TAG, "createFile dirFile.mkdirs fail");
                    return null;
                }
            } else if (!dirFile.isDirectory()) {
                boolean delete = dirFile.delete();
                if (delete) {
                    return createFile(dirPath, fileName);
                } else {
                    HdlLogLogic.print(TAG, "createFile dirFile !isDirectory and delete fail");
                    return null;
                }
            }
            File file = new File(dirPath, fileName);
            if (!file.exists()) {
                if (!file.createNewFile()) {
                    Log.e(TAG, "createFile createNewFile fail");
                    return null;
                }
            }
            return file;
        } catch (Exception e) {
            HdlLogLogic.print(TAG, "createFile fail :" + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * 创建文件夹---之所以要一层层创建,是因为一次性创建多层文件夹可能会失败!
     */
    public static boolean createFileDir(File dirFile) {
        if (dirFile == null) {
            return true;
        }
        if (dirFile.exists()) {
            return true;
        }
        File parentFile = dirFile.getParentFile();
        if (parentFile != null && !parentFile.exists()) {
            //父文件夹不存在,则先创建父文件夹,再创建自身文件夹
            return createFileDir(parentFile) && createFileDir(dirFile);
        } else {
            boolean mkdirs = dirFile.mkdirs();
            boolean isSuccess = mkdirs || dirFile.exists();
            if (!isSuccess) {
                Log.e("FileUtil", "createFileDir fail " + dirFile);
            }
            return isSuccess;
        }
    }
 
    public static void writeFile(String path, String data) {
        try {
            File file = new File(path);
            if (!file.exists()) {
                if (!file.mkdir() && !file.isDirectory()) {
                    HdlLogLogic.print(TAG, "Error: make dir failed!");
                    return;
                }
            }
            FileOutputStream d = new FileOutputStream(file);
            d.write(data.getBytes());
            d.flush();
            d.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
    public static 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[(int) f.length()];
            fis.read(bytes);
            fis.close();
            fileContent = new String(bytes, 0, bytes.length);
            return fileContent;
        } catch (Exception e1) {
            e1.printStackTrace();
            Log.d(TAG, "Error: Input File not find!");
            return "";
        }
 
    }
 
}