a
554325746@qq.com
2019-12-24 570a73851c26d810c2597596a8acc8a8d4cde211
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
package com.basic.security.utils;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
 
import com.basic.security.base.BaseApplication;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
 
public class FileUtil {
    public static boolean copyApkFromAssets(Context context, String fileName, String path) {
        boolean copyIsFinish = false;
        try {
            InputStream is = context.getAssets().open(fileName);
            File file = new File(path + "/" + fileName);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            byte[] temp = new byte[1024];
            int i;
            while ((i = is.read(temp)) > 0) {
                fos.write(temp, 0, i);
            }
            fos.close();
            is.close();
            copyIsFinish = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return copyIsFinish;
    }
 
    public static File writeToFile(String fileName, byte[] fileBytes) {
        try {
            return writeToFile(fileName, fileBytes, 0, fileBytes.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static File writeToFile(String fileName, byte[] fileBytes, int offset, int size) {
        try {
            if (fileBytes == null) {
                fileBytes = new byte[]{};
            }
            String dir = BaseApplication.getApplication().activity.getFilesDir().getAbsolutePath();
            File file = new File(dir, fileName);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(dir, fileName)));
            bos.write(fileBytes, offset, size);
            bos.flush();
            bos.close();
            return new File(dir, fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static File getFile(String shortFileName) {
        String dir = BaseApplication.getApplication().activity.getFilesDir().getAbsolutePath();
        return new File(dir, shortFileName);
    }
 
    public static File getPhotoFile() {
        File dir = new File(BaseApplication.getApplication().activity.getFilesDir().getAbsolutePath());
        File[] photoFiles = dir.listFiles();
        if (photoFiles != null) {
            for (File file : photoFiles) {
                if (file.isDirectory()) {
                    continue;
                }
                String name = file.getName();
                if (name.length() != "c248fe8f-6da5-4ec9-bffa-1a11bcf2fdc7".length()) {
                    continue;
                }
                if (file.isFile()) {
                    return file;
                }
            }
        }
        return null;
    }
 
    public static byte[] readFile1(String path) {
        File file = new File(path);
        int size = (int) file.length();
        byte[] bytes = new byte[size];
        try {
            BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
            buf.read(bytes, 0, bytes.length);
            buf.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }
 
    public static byte[] readFile(File file) {
        // Open file
        RandomAccessFile f = null;
        try {
            f = new RandomAccessFile(file, "r");
            // Get and check length
            long longlength = f.length();
            int length = (int) longlength;
            if (length != longlength)
                throw new IOException("File size >= 2 GB");
            // Read file and return data
            byte[] data = new byte[length];
            f.readFully(data);
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (f != null) {
                    f.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new byte[]{};
    }
 
    public static void deleteFile(String fileName) {
        try {
            String dir = BaseApplication.getApplication().activity.getFilesDir().getAbsolutePath();
            File file = new File(dir, fileName);
            if (file.exists()) {
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static String convertStreamToString(InputStream is) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        reader.close();
        return sb.toString();
    }
 
    public static String getStringFromFile(String filePath) {
        try {
            File fl = new File(filePath);
            FileInputStream fin = new FileInputStream(fl);
            String ret = convertStreamToString(fin);
            fin.close();
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static Bitmap loadBitmap(String fileName) {
        String dir = BaseApplication.getApplication().activity.getFilesDir().getAbsolutePath();
        File file = new File(dir, fileName);
        if (file.exists()) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        } else {
            return null;
        }
    }
}