里面注释很清楚了。。。

package cgjr.com.cgjr.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer; /**
* 文件管理类 在以后的开发中也可以使用这个工具类,提高代码的利用性
* 只要是对SD卡的操作
* 1、获取SD卡路径 getSDPATH
* 2、在SD卡上根据传入的目录名创建目录 createSDDir
* 3、在创建上目录后可以在该目录上创建文件 createSDFile
* 4、检测文件是否存在 isFileExist
* 5、将一个InputStream写入到SD卡中 write2SDFromInput
* 6、将一个字符流写入到SD卡 write2SDFromWrite
* 注:如果要写入SD卡,只要调用write2SDFromInput函数即可
*
* @author Administrator
*/
public class FileUtils {
private static String SDPATH;
private static final String TAG = "FileUtils"; public FileUtils() {
//得到当前设备外部存储设备的目录
SDPATH = Environment.getExternalStorageDirectory() + File.separator;
} /**
* 获取当前SD卡的根目录
*
* @return
*/
public String getSDPATH() {
return SDPATH;
} /**
* SD卡上创建目录
*/
public File createSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
Log.i(TAG, "createSDDir " + SDPATH + dirName);
if (!dir.exists()) {
dir.mkdirs();
}
return dir;
} /**
* SD卡上创建文件
*/
public File createSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
Log.i(TAG, "createSDFile " + SDPATH + fileName);
file.createNewFile();
return file;
} /**
* 判断SD卡上的文件是否存在
*/
public boolean isFileExist(String fileName) {
File file = new File(SDPATH + fileName);
return file.exists();
} /**
* 将一个InputStream字节流写入到SD卡中
*/
public File write2SDFromInput(String Path, String FileName, InputStream input) {
File file = null;
OutputStream output = null; //创建一个写入字节流对象
try {
createSDDir(Path); //根据传入的路径创建目录
file = createSDFile(Path + FileName); //根据传入的文件名创建
output = new FileOutputStream(file);
byte buffer[] = new byte[4 * 1024]; //每次读取4K
int num = 0; //需要根据读取的字节大小写入文件
while ((num = (input.read(buffer))) != -1) {
output.write(buffer, 0, num);
}
output.flush(); //清空缓存
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (output != null)
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
} /**
* 把传入的字符流写入到SD卡中
*
* @param Path
* @param FileName
* @param input
* @return
*/
public File write2SDFromWrite(String Path, String FileName, BufferedReader input) {
File file = null;
FileWriter output = null; //创建一个写入字符流对象
BufferedWriter bufw = null;
try {
createSDDir(Path); //根据传入的路径创建目录
file = createSDFile(Path + FileName); //根据传入的文件名创建
output = new FileWriter(file);
bufw = new BufferedWriter(output);
String line = null;
while ((line = (input.readLine())) != null) {
bufw.write(line);
bufw.newLine();
}
bufw.flush(); //清空缓存
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bufw != null)
bufw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
} /**
* 从文本文件对象中读取内容并转换为字符数组
*
* @param file File 对象
* @return 读到的字符数据
*/
public static char[] readChars(File file) {
CharArrayWriter caw = new CharArrayWriter();
try {
Reader fr = new FileReader(file);
Reader in = new BufferedReader(fr);
int count = 0;
char[] buf = new char[16384];
while ((count = in.read(buf)) != -1) {
if (count > 0) caw.write(buf, 0, count);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return caw.toCharArray();
} /**
* 从字符串对象中读取内容并转换为字符数组
*
* @param string 在读的String数据
* @return 字符数组
*/
public static char[] readChars(String string) {
CharArrayWriter caw = new CharArrayWriter();
try {
Reader sr = new StringReader(string.trim());
Reader in = new BufferedReader(sr);
int count = 0;
char[] buf = new char[16384];
while ((count = in.read(buf)) != -1) {
if (count > 0) caw.write(buf, 0, count);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return caw.toCharArray();
} /**
* 从二进制文件对象中读取内容并转换为字节数组
*
* @param file 要读取的File对象
* @return 读取后的字节数据
*/
public static byte[] readBytes(File file) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
InputStream fis = new FileInputStream(file);
InputStream is = new BufferedInputStream(fis);
int count = 0;
byte[] buf = new byte[16384];
while ((count = is.read(buf)) != -1) {
if (count > 0) baos.write(buf, 0, count);
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return baos.toByteArray();
} /**
* 写字节数组内容到二进制文件
*
* @param file File对象
* @param data 输出字节数组
*/
public static void writeBytes(File file, byte[] data) {
try {
OutputStream fos = new FileOutputStream(file);
OutputStream os = new BufferedOutputStream(fos);
os.write(data);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 写字符数组内容到文本文件
*
* @param file File对象
* @param data 输出字节数组
*/
public static void writeChars(File file, char[] data) {
try {
Writer fos = new FileWriter(file);
Writer os = new BufferedWriter(fos);
os.write(data);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Environment.getDataDirectory() +path 读取文件
*
* @see #localWriter(Bitmap, String, String)
*/
public static Bitmap localReader(String name, String path) {
File fileRe = null;
try {
File dataDirectory = Environment.getExternalStorageDirectory();
if (dataDirectory.exists()) {
fileRe = new File(dataDirectory.getPath() + File.separator + path + File.separator + name);
// 文件不存在
if (fileRe == null || !fileRe.exists()) {
return null;
} else {
return BitmapFactory.decodeFile(fileRe.getPath());
}
}
} catch (Exception e) {
return null;
}
return null;
} public static Bitmap localReaderByPath(String name, String path,Context context) {
File fileRe = null; try {
fileRe = new File(path + File.separator + MD5Util.md5(name));
// 文件不存在
if (fileRe == null || !fileRe.exists()) {
return null;
} else {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(fileRe.getPath(), options);
int width = options.outWidth;
int height = options.outHeight;
int inSampleSize = 1;
int size = width /context.getResources().getDisplayMetrics().widthPixels;
if (size > 0) {
inSampleSize = size;
}
Log.i("AsyncImageLoader", "height is: " + height + " width is: " + width + "sampleSize: " + inSampleSize);
options.inPurgeable = true;
options.inInputShareable = true;
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(fileRe.getPath(),options);
}
} catch (Exception e) {
return null;
} } /**
* Environment.getDataDirectory()
*
* @param bm
* @param name
* @param path
* @return
* @see #localReader(String, String)
*/
public static boolean localWriter(Bitmap bm, String name, String path) {
File dataDirectory = Environment.getExternalStorageDirectory();
try {
if (dataDirectory.exists()) {
String s = dataDirectory.getPath() + File.separator + path + File.separator;
File write = new File(s);
if (!write.exists()) {
if (write.mkdirs()) {
FileOutputStream fileOutputStream = new FileOutputStream(new File(s + MD5Util.md5(name)));
fileOutputStream.write(StreamUtils.bitmap2Bytes(bm));
fileOutputStream.close();
}
} else {
FileOutputStream fileOutputStream = new FileOutputStream(new File(s + MD5Util.md5(name)));
fileOutputStream.write(StreamUtils.bitmap2Bytes(bm));
fileOutputStream.close();
}
}
} catch (Exception e) {
return false;
}
return true;
} /**
* Environment.getDataDirectory()
*
* @param bm
* @param name
* @param path
* @return
* @see #localReader(String, String)
*/
public static boolean localWriterByPath(Bitmap bm, String name, String path) {
try {
String s = path + File.separator;
File write = new File(s);
if (!write.exists()) {
if (write.mkdirs()) {
FileOutputStream fileOutputStream = new FileOutputStream(new File(s + MD5Util.md5(name)));
fileOutputStream.write(StreamUtils.bitmap2Bytes(bm));
fileOutputStream.close();
}
} else {
FileOutputStream fileOutputStream = new FileOutputStream(new File(s + MD5Util.md5(name)));
fileOutputStream.write(StreamUtils.bitmap2Bytes(bm));
fileOutputStream.close();
}
} catch (Exception e) {
return false;
}
return true;
}
}

工具类总结---(五)---SD卡文件管理的更多相关文章

  1. JQuery中的工具类(五)

    一:1.serialize()序列表表格内容为字符串.返回值jQuery示例序列表表格内容为字符串,用于 Ajax 请求. HTML 代码:<p id="results"&g ...

  2. 并发工具类(五) Phaser类

    前言   JDK中为了处理线程之间的同步问题,除了提供锁机制之外,还提供了几个非常有用的并发工具类:CountDownLatch.CyclicBarrier.Semphore.Exchanger.Ph ...

  3. IntentActionUtil【Intent的常见作用的工具类】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 主要用于通过Intent调用手机本地软件打开文件(doc.xsl.pdf.ppt.mp3.mp4等格式).安装apk.发送邮件.拨打 ...

  4. Android工具类整合

    Android-JSONUtil工具类 常用的Json工具类,包含Json转换成实体.实体转json字符串.list集合转换成json.数组转换成json public class JSONUtil ...

  5. WP8.1 Study12:文件压缩与Known Folder(包含SD卡操作)

    一.文件压缩 当应用程序保存和加载数据,它可以使用压缩. 1.使用 Windows.Storage.Compression.Compressor 压缩,获得一个Compressor stream. v ...

  6. 基于Dapper二次封装了一个易用的ORM工具类:SqlDapperUtil

    基于Dapper二次封装了一个易用的ORM工具类:SqlDapperUtil,把日常能用到的各种CRUD都进行了简化封装,让普通程序员只需关注业务即可,因为非常简单,故直接贴源代码,大家若需使用可以直 ...

  7. BoneBlack am335x利用SD卡烧写板卡上的emmc

    参考ti论坛上面的一篇文章: 链接:https://pan.baidu.com/s/1SLSUbCRrIULJJf_BNI3sEQ 密码: hvem 自己稍微修改的debrick.sh 链接: htt ...

  8. 2019 SD卡、U盘无法格式化怎么办的解决方法

    有天 闲的没事, 格式化一下U盘 ,结果突然断电了,我的天.我还在格式化的U盘 ,果然 ,我在此启动电脑后,的U盘直接 就不能用了.于是 我格式化. 然后,我的U盘就怎么也格式化不好了 ,找到了几种解 ...

  9. JavaScript工具类(三):localStorage本地储存

    localStorage Web 存储 API 提供了 sessionStorage (会话存储) 和 localStorage(本地存储)两个存储对象来对网页的数据进行添加.删除.修改.查询操作. ...

随机推荐

  1. MVC学习笔记1-MVC家族间的区别

    ASP.NET下的MVC从原始的1.0走到2.0,再到3.0,现在走到4.0,也许明年5.0就问世了,先不管那些,那说说这些MVC在ASP.NET是如何变化发展的.对于.net编程人员来说可能会很熟悉 ...

  2. SEQ序号与ACK序号理解总结

    备查 SEQ序号与ACK序号理解总结

  3. Lvs工作原理

    DR模式的工作过程: 当一个client发送一个WEB请求到VIP,LVS服务器根据VIP选择对应的real-server的Pool,根据算法,在Pool中选择一台Real-server,LVS在ha ...

  4. HDU-2298 Toxophily (三分法入门系列)

    题意: 意大利炮射出炮弹的速度为v,求在(0,0)击中(x,y)处的目标,发射炮弹的角度. 题解: 设f(α)表示角度为α时, f(α) = vsin(α) * t - 4.9 * t * t   ① ...

  5. Luogu3373【模板】线段树2

    P3373[模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格式: 第一行 ...

  6. Java进阶之内存管理与垃圾回收

    Java是在JVM所虚拟出的内存环境中运行的.内存分为栈(stack)和堆(heap)两部分.我们将分别考察这两个区域. 栈 在Java中,JVM中的栈记录了线程的方法调用.每个线程拥有一个栈.在某个 ...

  7. CSS3学习笔记(1)-CSS3选择器

    p{ font-size: 15px; text-indent: 2em; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid ...

  8. Java并发编程:volatile 关键字

    转自:http://www.cnblogs.com/aigongsi/archive/2012/04/01/2429166.html 其实Java语言是支持多线程的,为了解决线程并发的问题,在语言内部 ...

  9. 《JavaScript程序设计》第2课:JS类型系统

    JS类型系统可以分为标准类型和对象类型,进一步标准类型又可以分为原始类型和引用类型,而对象类型又可以分为内置对象类型.普通对象类型.自定义对象类型. 1. 标准类型 标准类型共包括了6个分别是:und ...

  10. 【G】开源的分布式部署解决方案文档 - Web Deploy

    G.系列导航 [G]开源的分布式部署解决方案 - 导航 微软官方部署方式 右键项目->发布 这个大家应该再熟悉不过,在部署前有个预览界面可以看本次更新到底更新哪些文件. 既然它可以预览部署结果, ...