/
FileUtils.java
147 lines (125 loc) · 4.33 KB
/
FileUtils.java
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
package com.sloop.io.utils;
import android.content.Context;
import android.os.Environment;
import java.io.File;
/**
* 与文件相关的工具类
* Author: Sloop
* Version: v1.1
* Date: 2015/11/18
* <ul type="disc">
* <li><a href="http://www.sloop.icoc.cc" target="_blank">作者网站</a> </li>
* <li><a href="http://weibo.com/5459430586" target="_blank">作者微博</a> </li>
* <li><a href="https://github.com/GcsSloop" target="_blank">作者GitHub</a> </li>
* </ul>
*/
public class FileUtils {
private FileUtils() {
}
//****系统文件目录**********************************************************************************************
/**
* @return 程序系统文件目录
*/
public static String getFileDir(Context context) {
return String.valueOf(context.getFilesDir());
}
/**
* @param context 上下文
* @param customPath 自定义路径
* @return 程序系统文件目录绝对路径
*/
public static String getFileDir(Context context, String customPath) {
String path = context.getFilesDir() + formatPath(customPath);
mkdir(path);
return path;
}
//****系统缓存目录**********************************************************************************************
/**
* @return 程序系统缓存目录
*/
public static String getCacheDir(Context context) {
return String.valueOf(context.getCacheDir());
}
/**
* @param context 上下文
* @param customPath 自定义路径
* @return 程序系统缓存目录
*/
public static String getCacheDir(Context context, String customPath) {
String path = context.getCacheDir() + formatPath(customPath);
mkdir(path);
return path;
}
//****Sdcard文件目录**********************************************************************************************
/**
* @return 内存卡文件目录
*/
public static String getExternalFileDir(Context context) {
return String.valueOf(context.getExternalFilesDir(""));
}
/**
* @param context 上下文
* @param customPath 自定义路径
* @return 内存卡文件目录
*/
public static String getExternalFileDir(Context context, String customPath) {
String path = context.getExternalFilesDir("") + formatPath(customPath);
mkdir(path);
return path;
}
//****Sdcard缓存目录**********************************************************************************************
/**
* @return 内存卡缓存目录
*/
public static String getExternalCacheDir(Context context) {
return String.valueOf(context.getExternalCacheDir());
}
/**
* @param context 上下文
* @param customPath 自定义路径
* @return 内存卡缓存目录
*/
public static String getExternalCacheDir(Context context, String customPath) {
String path = context.getExternalCacheDir() + formatPath(customPath);
mkdir(path);
return path;
}
//****公共文件夹**********************************************************************************************
/**
* @return 公共下载文件夹
*/
public static String getPublicDownloadDir() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
}
//****相关工具**********************************************************************************************
/**
* 创建文件夹
*
* @param DirPath 文件夹路径
*/
public static void mkdir(String DirPath) {
File file = new File(DirPath);
if (!(file.exists() && file.isDirectory())) {
file.mkdirs();
}
}
/**
* 格式化文件路径
* 示例: 传入 "sloop" "/sloop" "sloop/" "/sloop/"
* 返回 "/sloop"
*/
private static String formatPath(String path) {
if (!path.startsWith("/"))
path = "/" + path;
while (path.endsWith("/"))
path = new String(path.toCharArray(), 0, path.length() - 1);
return path;
}
/**
* @return 存储卡是否挂载(存在)
*/
public static boolean isMountSdcard() {
String status = Environment.getExternalStorageState();
return status.equals(Environment.MEDIA_MOUNTED);
}
}