Android文件操作(SD卡、缓存)工具类
2016-06-19 15:07
519 查看
public class FileUtils {
public static String getDataFolderPath(Context paramContext) {
return Environment.getDataDirectory() + "/data/" + paramContext.getPackageName() + "/files";
}
public static String getMyFileDir(Context context){
return context.getFilesDir().toString();
}
public static String getMyCacheDir(Context context){
return context.getCacheDir().toString();
}
/** * @desc 保存内容到文件中 * @param fileName * @param content * @throws Exception */ public static void save(Context context, String fileName, String content, int module) {
try {
FileOutputStream os = context.openFileOutput(fileName, module);
os.write(content.getBytes());
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/** * @desc 读取文件内容 * @param fileName * @return */ public static String read(Context context, String fileName){
try {
FileInputStream fis = context.openFileInput(fileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len = 0;
while((len = fis.read(b)) != -1){
bos.write(b, 0, len);
}
byte[] data = bos.toByteArray();
fis.close();
bos.close();
return new String(data);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/** * @desc 将文本内容保存到sd卡的文件中 * @param context * @param fileName * @param content * @throws IOException */ public static void saveToSDCard(Context context, String fileName, String content) throws IOException {
File file = new File(Environment.getExternalStorageDirectory(),fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(content.getBytes());
fos.close();
}
/** * @desc 读取sd卡文件内容 * @param fileName * @return * @throws IOException */ public static String readSDCard(String fileName) throws IOException {
File file = new File(Environment.getExternalStorageDirectory(),fileName);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer)) != -1)
{
bos.write(buffer, 0, len);
}
byte[] data = bos.toByteArray();
fis.close();
bos.close();
return new String(data);
}
/*获取缓存路径,存储临时文件,可被一键清理和卸载清理*/ /* * 可以看到,当SD卡存在或者SD卡不可被移除的时候, * 就调用getExternalCacheDir()方法来获取缓存路径, * 否则就调用getCacheDir()方法来获取缓存路径。 * 前者获取到的就是/sdcard/Android/data/<application package>/cache 这个路径, * 而后者获取到的是 /data/data/<application package>/cache 这个路径。*/ public static File getDiskCacheDir(Context context, String uniqueName) {
String cachePath;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getExternalCacheDir().getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
return new File(cachePath + File.separator + uniqueName);
}
/*返回缓存路径*/ public static File getDiskCacheDir(Context context) {
String cachePath;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getExternalCacheDir().getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
return new File(cachePath);
}
/*判断sd卡可用*/ public static boolean hasSDCardMounted() {
String state = Environment.getExternalStorageState();
if (state != null && state.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
参考链接 http://www.voidcn.com/blog/wangshihui512/article/p-5044431.html
public static String getDataFolderPath(Context paramContext) {
return Environment.getDataDirectory() + "/data/" + paramContext.getPackageName() + "/files";
}
public static String getMyFileDir(Context context){
return context.getFilesDir().toString();
}
public static String getMyCacheDir(Context context){
return context.getCacheDir().toString();
}
/** * @desc 保存内容到文件中 * @param fileName * @param content * @throws Exception */ public static void save(Context context, String fileName, String content, int module) {
try {
FileOutputStream os = context.openFileOutput(fileName, module);
os.write(content.getBytes());
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/** * @desc 读取文件内容 * @param fileName * @return */ public static String read(Context context, String fileName){
try {
FileInputStream fis = context.openFileInput(fileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len = 0;
while((len = fis.read(b)) != -1){
bos.write(b, 0, len);
}
byte[] data = bos.toByteArray();
fis.close();
bos.close();
return new String(data);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/** * @desc 将文本内容保存到sd卡的文件中 * @param context * @param fileName * @param content * @throws IOException */ public static void saveToSDCard(Context context, String fileName, String content) throws IOException {
File file = new File(Environment.getExternalStorageDirectory(),fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(content.getBytes());
fos.close();
}
/** * @desc 读取sd卡文件内容 * @param fileName * @return * @throws IOException */ public static String readSDCard(String fileName) throws IOException {
File file = new File(Environment.getExternalStorageDirectory(),fileName);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer)) != -1)
{
bos.write(buffer, 0, len);
}
byte[] data = bos.toByteArray();
fis.close();
bos.close();
return new String(data);
}
/*获取缓存路径,存储临时文件,可被一键清理和卸载清理*/ /* * 可以看到,当SD卡存在或者SD卡不可被移除的时候, * 就调用getExternalCacheDir()方法来获取缓存路径, * 否则就调用getCacheDir()方法来获取缓存路径。 * 前者获取到的就是/sdcard/Android/data/<application package>/cache 这个路径, * 而后者获取到的是 /data/data/<application package>/cache 这个路径。*/ public static File getDiskCacheDir(Context context, String uniqueName) {
String cachePath;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getExternalCacheDir().getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
return new File(cachePath + File.separator + uniqueName);
}
/*返回缓存路径*/ public static File getDiskCacheDir(Context context) {
String cachePath;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getExternalCacheDir().getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
return new File(cachePath);
}
/*判断sd卡可用*/ public static boolean hasSDCardMounted() {
String state = Environment.getExternalStorageState();
if (state != null && state.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
参考链接 http://www.voidcn.com/blog/wangshihui512/article/p-5044431.html
相关文章推荐
- Android中数据库的使用
- Android 获取并显示远程图片 Picasso框架的使用
- android中自定义的dialog中的EditText无法弹出输入法解决方案
- Android入门——Fragment详解之Fragment与Activity通信及数据交换(二)
- Android 进程间通信
- android你可能要用到的自定义View分享
- Android新特性之CardView的简单使用
- 转: Android基于HLS和RTMP协议的第三方SDK选择
- 给 Android 开发者的 RxJava 详解
- android5.0之animated-selector
- android中的:scaleType属性
- Android系统系统升级过程分析之------update.zip包的制作
- Android中Services之异步IntentService
- 用Chrome来调试你的Android应用
- 深入学习activity之不同情况的生命周期
- 《老罗的Android之旅》导读PPT
- Android开发 10个常用工具类
- Android使用Xfermode图形渲染方法实现自定义圆形、圆角和椭圆ImageView
- Android-防止用户快速点击和多控件点击
- Android开发常用代码片段