haijiang
2018-06-25 586f13d3aa93fc3fdfed65021b1a17a17acf3321
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package com.moral.andbrickslib.utils;
 
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
 
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
/**
 * 文件类
 * author hjzhang
 */
public class FileUtils {
    public static boolean makeDirs(String filePath) {
        String folderName = getFolderName(filePath);
        if (StringUtils.isEmpty(folderName)) {
            return false;
        }
        File folder = new File(folderName);
        return (folder.exists() && folder.isDirectory()) ? true : folder.mkdirs();
    }
    public static String getFolderName(String filePath) {
 
        if (StringUtils.isEmpty(filePath)) {
            return filePath;
        }
 
        int filePosi = filePath.lastIndexOf(File.separator);
        return (filePosi == -1) ? "" : filePath.substring(0, filePosi);
    }
    /**
     * @param filePath
     * @return
     * @see #makeDirs(String)
     */
    public static boolean makeFolders(String filePath) {
        return makeDirs(filePath);
    }
 
    public static boolean makeFolders(File filePath) {
        return makeDirs(filePath.getAbsolutePath());
    }
 
 
    /**
     * 文件是否存在
     *
     * @param filePath
     * @return
     */
    public static boolean exists(String filePath) {
        if (filePath == null || filePath.length() < 1) {
            return false;
        }
 
        File f = new File(filePath);
        if (!f.exists()) {
            return false;
        }
        return true;
    }
 
    /**
     * 创建文件路径
     *
     * @param filePath 文件路径
     * @return 成功true,失败false
     */
    public static boolean mkdirs(String filePath) {
        if (null == filePath) {
            return false;
        }
 
        File file = new File(filePath);
 
        if (file.exists()) {
            return true;
        }
        return file.mkdirs();
    }
 
    /**
     * 删除文件夹
     *
     * @param filePath 文件夹路径
     * @return
     */
    public static boolean deleteDir(String filePath) {
        if (null == filePath) {
            return false;
        }
 
        File file = new File(filePath);
 
        if (file == null || !file.exists()) {
            return false;
        }
 
        if (file.isDirectory()) {
            File[] list = file.listFiles();
 
            for (int i = 0; i < list.length; i++) {
                if (list[i].isDirectory()) {
                    deleteDir(list[i].getAbsolutePath());
                } else {
                    list[i].delete();
                }
            }
        }
        file.delete();
        return true;
    }
 
    /**
     * 获取SD存储路径
     *
     * @return 路径
     */
    public static String getSDFilePath() {
        return Environment.getExternalStorageDirectory().getAbsolutePath() + "/";// filePath:/sdcard/
    }
 
    /**
     * 获取内部存储路径
     *
     * @return 路径
     */
    public static String getRootFilePath() {
        return Environment.getDataDirectory().getAbsolutePath() + "/data/" + "com.ps.app" + "/"; // filePath: /data/data/
    }
 
    /**
     * 获取存储主路径
     *
     * @return
     */
    public static String getFilePath() {
        if (hasSDCard()) {
            return getSDFilePath();
        }
        return getRootFilePath();
    }
 
    /**
     * 判断是否有SD卡
     *
     * @return 是true ,否false
     */
    public static boolean hasSDCard() {
        String status = Environment.getExternalStorageState();
        if (!status.equals(Environment.MEDIA_MOUNTED)) {
            return false;
        }
        return true;
    }
 
    /**
     * 获取图片保存路径
     *
     * @return
     */
    public static String getImgPath() {
        String ImgPath = null;
        ImgPath = getFilePath() + "ps/picture/";
        if (!exists(ImgPath)) {
            mkdirs(ImgPath);
        }
        return ImgPath;
    }
    
    /**
     * 获取录音保存路径
     *
     * @return
     */
    public static String getVoicePath() {
        String voicePath = null;
        voicePath = getFilePath() + "ps/voice/";
        if (!exists(voicePath)) {
            mkdirs(voicePath);
        }
        return voicePath;
    }
    
    /**
     * 获取字符串 保存路径
     *
     * @return
     */
    public static String getStringPath() {
        String strPath = null;
        strPath = getFilePath() + "ps/str/";
        if (!exists(strPath)) {
            mkdirs(strPath);
        }
        return strPath;
    }
 
    /**
     * 读取图片文件转成Bitmap
     *
     * @param path
     * @return
     */
    public static Bitmap getBitmapFromFile(String path) {
        Bitmap b = null;
        b = BitmapFactory.decodeFile(path);
        return b;
    }
 
    /**
     * 将String数据存为文件
     */
    public static File getFileFromBytes(String name, String path) {
        byte[] b = name.getBytes();
        BufferedOutputStream stream = null;
        File file = null;
        try {
            file = new File(path);
            FileOutputStream fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return file;
    }
    
    /**
     * 将本地相册的Uri转成绝对路径
     *
     * @param activity 上下文
     * @param uri      本地相册图片Uri
     * @return 图片所在的文件路径
     */
    public static String UriToPath(Activity activity, Uri uri) {
        String[] proj = {MediaStore.Images.Media.DATA};
 
        Cursor actualimagecursor = activity.managedQuery(uri, proj, null, null, null);
 
        int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
 
        actualimagecursor.moveToFirst();
 
        String path = actualimagecursor.getString(actual_image_column_index);
        return path;
    }
    
    /**
     * 关闭流
     * 
     * @param closeables
     */
    public static void closeIO(Closeable... closeables) {
        if (null == closeables || closeables.length <= 0) {
            return;
        }
        for (Closeable cb : closeables) {
            try {
                if (null == cb) {
                    continue;
                }
                cb.close();
            } catch (IOException e) {
                throw new RuntimeException(
                        FileUtils.class.getClass().getName(), e);
            }
        }
    }
    
    /**
     * 从文件中读取文本
     * 
     * @param filePath
     * @return
     */
    public static String readFile(String filePath) {
        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
        } catch (Exception e) {
            throw new RuntimeException(FileUtils.class.getName()
                    + "readFile---->" + filePath + " not found");
        }
        return inputStream2String(is);
    }
 
    /**
     * 从assets中读取文本
     * 
     * @param name
     * @return
     */
    public static String readFileFromAssets(Context context, String name) {
        InputStream is = null;
        try {
            is = context.getResources().getAssets().open(name);
        } catch (Exception e) {
            throw new RuntimeException(FileUtils.class.getName()
                    + ".readFileFromAssets---->" + name + " not found");
        }
        return inputStream2String(is);
    }
 
    /**
     * 输入流转字符串
     * 
     * @param is
     * @return 一个流中的字符串
     */
    public static String inputStream2String(InputStream is) {
        if (null == is) {
            return null;
        }
        StringBuilder resultSb = null;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            resultSb = new StringBuilder();
            String len;
            while (null != (len = br.readLine())) {
                resultSb.append(len);
            }
        } catch (Exception ex) {
        } finally {
            closeIO(is);
        }
        return null == resultSb ? null : resultSb.toString();
    }
}