里面注释很清楚了。。。

  1. package cgjr.com.cgjr.utils;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.os.Environment;
  7. import android.util.Log;
  8.  
  9. import java.io.BufferedInputStream;
  10. import java.io.BufferedOutputStream;
  11. import java.io.BufferedReader;
  12. import java.io.BufferedWriter;
  13. import java.io.ByteArrayOutputStream;
  14. import java.io.CharArrayWriter;
  15. import java.io.File;
  16. import java.io.FileInputStream;
  17. import java.io.FileOutputStream;
  18. import java.io.FileReader;
  19. import java.io.FileWriter;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.io.Reader;
  24. import java.io.StringReader;
  25. import java.io.Writer;
  26.  
  27. /**
  28. * 文件管理类 在以后的开发中也可以使用这个工具类,提高代码的利用性
  29. * 只要是对SD卡的操作
  30. * 1、获取SD卡路径 getSDPATH
  31. * 2、在SD卡上根据传入的目录名创建目录 createSDDir
  32. * 3、在创建上目录后可以在该目录上创建文件 createSDFile
  33. * 4、检测文件是否存在 isFileExist
  34. * 5、将一个InputStream写入到SD卡中 write2SDFromInput
  35. * 6、将一个字符流写入到SD卡 write2SDFromWrite
  36. * 注:如果要写入SD卡,只要调用write2SDFromInput函数即可
  37. *
  38. * @author Administrator
  39. */
  40. public class FileUtils {
  41. private static String SDPATH;
  42. private static final String TAG = "FileUtils";
  43.  
  44. public FileUtils() {
  45. //得到当前设备外部存储设备的目录
  46. SDPATH = Environment.getExternalStorageDirectory() + File.separator;
  47. }
  48.  
  49. /**
  50. * 获取当前SD卡的根目录
  51. *
  52. * @return
  53. */
  54. public String getSDPATH() {
  55. return SDPATH;
  56. }
  57.  
  58. /**
  59. * SD卡上创建目录
  60. */
  61. public File createSDDir(String dirName) {
  62. File dir = new File(SDPATH + dirName);
  63. Log.i(TAG, "createSDDir " + SDPATH + dirName);
  64. if (!dir.exists()) {
  65. dir.mkdirs();
  66. }
  67. return dir;
  68. }
  69.  
  70. /**
  71. * SD卡上创建文件
  72. */
  73. public File createSDFile(String fileName) throws IOException {
  74. File file = new File(SDPATH + fileName);
  75. Log.i(TAG, "createSDFile " + SDPATH + fileName);
  76. file.createNewFile();
  77. return file;
  78. }
  79.  
  80. /**
  81. * 判断SD卡上的文件是否存在
  82. */
  83. public boolean isFileExist(String fileName) {
  84. File file = new File(SDPATH + fileName);
  85. return file.exists();
  86. }
  87.  
  88. /**
  89. * 将一个InputStream字节流写入到SD卡中
  90. */
  91. public File write2SDFromInput(String Path, String FileName, InputStream input) {
  92. File file = null;
  93. OutputStream output = null; //创建一个写入字节流对象
  94. try {
  95. createSDDir(Path); //根据传入的路径创建目录
  96. file = createSDFile(Path + FileName); //根据传入的文件名创建
  97. output = new FileOutputStream(file);
  98. byte buffer[] = new byte[4 * 1024]; //每次读取4K
  99. int num = 0; //需要根据读取的字节大小写入文件
  100. while ((num = (input.read(buffer))) != -1) {
  101. output.write(buffer, 0, num);
  102. }
  103. output.flush(); //清空缓存
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. } finally {
  107. try {
  108. if (output != null)
  109. output.close();
  110. } catch (Exception e) {
  111. e.printStackTrace();
  112. }
  113. }
  114. return file;
  115. }
  116.  
  117. /**
  118. * 把传入的字符流写入到SD卡中
  119. *
  120. * @param Path
  121. * @param FileName
  122. * @param input
  123. * @return
  124. */
  125. public File write2SDFromWrite(String Path, String FileName, BufferedReader input) {
  126. File file = null;
  127. FileWriter output = null; //创建一个写入字符流对象
  128. BufferedWriter bufw = null;
  129. try {
  130. createSDDir(Path); //根据传入的路径创建目录
  131. file = createSDFile(Path + FileName); //根据传入的文件名创建
  132. output = new FileWriter(file);
  133. bufw = new BufferedWriter(output);
  134. String line = null;
  135. while ((line = (input.readLine())) != null) {
  136. bufw.write(line);
  137. bufw.newLine();
  138. }
  139. bufw.flush(); //清空缓存
  140. } catch (Exception e) {
  141. e.printStackTrace();
  142. } finally {
  143. try {
  144. if (bufw != null)
  145. bufw.close();
  146. } catch (Exception e) {
  147. e.printStackTrace();
  148. }
  149. }
  150. return file;
  151. }
  152.  
  153. /**
  154. * 从文本文件对象中读取内容并转换为字符数组
  155. *
  156. * @param file File 对象
  157. * @return 读到的字符数据
  158. */
  159. public static char[] readChars(File file) {
  160. CharArrayWriter caw = new CharArrayWriter();
  161. try {
  162. Reader fr = new FileReader(file);
  163. Reader in = new BufferedReader(fr);
  164. int count = 0;
  165. char[] buf = new char[16384];
  166. while ((count = in.read(buf)) != -1) {
  167. if (count > 0) caw.write(buf, 0, count);
  168. }
  169. in.close();
  170. } catch (Exception e) {
  171. e.printStackTrace();
  172. }
  173. return caw.toCharArray();
  174. }
  175.  
  176. /**
  177. * 从字符串对象中读取内容并转换为字符数组
  178. *
  179. * @param string 在读的String数据
  180. * @return 字符数组
  181. */
  182. public static char[] readChars(String string) {
  183. CharArrayWriter caw = new CharArrayWriter();
  184. try {
  185. Reader sr = new StringReader(string.trim());
  186. Reader in = new BufferedReader(sr);
  187. int count = 0;
  188. char[] buf = new char[16384];
  189. while ((count = in.read(buf)) != -1) {
  190. if (count > 0) caw.write(buf, 0, count);
  191. }
  192. in.close();
  193. } catch (Exception e) {
  194. e.printStackTrace();
  195. }
  196. return caw.toCharArray();
  197. }
  198.  
  199. /**
  200. * 从二进制文件对象中读取内容并转换为字节数组
  201. *
  202. * @param file 要读取的File对象
  203. * @return 读取后的字节数据
  204. */
  205. public static byte[] readBytes(File file) {
  206. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  207. try {
  208. InputStream fis = new FileInputStream(file);
  209. InputStream is = new BufferedInputStream(fis);
  210. int count = 0;
  211. byte[] buf = new byte[16384];
  212. while ((count = is.read(buf)) != -1) {
  213. if (count > 0) baos.write(buf, 0, count);
  214. }
  215. is.close();
  216. } catch (Exception e) {
  217. e.printStackTrace();
  218. }
  219. return baos.toByteArray();
  220. }
  221.  
  222. /**
  223. * 写字节数组内容到二进制文件
  224. *
  225. * @param file File对象
  226. * @param data 输出字节数组
  227. */
  228. public static void writeBytes(File file, byte[] data) {
  229. try {
  230. OutputStream fos = new FileOutputStream(file);
  231. OutputStream os = new BufferedOutputStream(fos);
  232. os.write(data);
  233. os.close();
  234. } catch (Exception e) {
  235. e.printStackTrace();
  236. }
  237. }
  238.  
  239. /**
  240. * 写字符数组内容到文本文件
  241. *
  242. * @param file File对象
  243. * @param data 输出字节数组
  244. */
  245. public static void writeChars(File file, char[] data) {
  246. try {
  247. Writer fos = new FileWriter(file);
  248. Writer os = new BufferedWriter(fos);
  249. os.write(data);
  250. os.close();
  251. } catch (Exception e) {
  252. e.printStackTrace();
  253. }
  254. }
  255.  
  256. /**
  257. * Environment.getDataDirectory() +path 读取文件
  258. *
  259. * @see #localWriter(Bitmap, String, String)
  260. */
  261. public static Bitmap localReader(String name, String path) {
  262. File fileRe = null;
  263. try {
  264. File dataDirectory = Environment.getExternalStorageDirectory();
  265. if (dataDirectory.exists()) {
  266. fileRe = new File(dataDirectory.getPath() + File.separator + path + File.separator + name);
  267. // 文件不存在
  268. if (fileRe == null || !fileRe.exists()) {
  269. return null;
  270. } else {
  271. return BitmapFactory.decodeFile(fileRe.getPath());
  272. }
  273. }
  274. } catch (Exception e) {
  275. return null;
  276. }
  277. return null;
  278. }
  279.  
  280. public static Bitmap localReaderByPath(String name, String path,Context context) {
  281. File fileRe = null;
  282.  
  283. try {
  284. fileRe = new File(path + File.separator + MD5Util.md5(name));
  285. // 文件不存在
  286. if (fileRe == null || !fileRe.exists()) {
  287. return null;
  288. } else {
  289. BitmapFactory.Options options = new BitmapFactory.Options();
  290. options.inJustDecodeBounds = true;
  291. BitmapFactory.decodeFile(fileRe.getPath(), options);
  292. int width = options.outWidth;
  293. int height = options.outHeight;
  294. int inSampleSize = 1;
  295. int size = width /context.getResources().getDisplayMetrics().widthPixels;
  296. if (size > 0) {
  297. inSampleSize = size;
  298. }
  299. Log.i("AsyncImageLoader", "height is: " + height + " width is: " + width + "sampleSize: " + inSampleSize);
  300. options.inPurgeable = true;
  301. options.inInputShareable = true;
  302. options.inSampleSize = inSampleSize;
  303. options.inJustDecodeBounds = false;
  304. return BitmapFactory.decodeFile(fileRe.getPath(),options);
  305. }
  306. } catch (Exception e) {
  307. return null;
  308. }
  309.  
  310. }
  311.  
  312. /**
  313. * Environment.getDataDirectory()
  314. *
  315. * @param bm
  316. * @param name
  317. * @param path
  318. * @return
  319. * @see #localReader(String, String)
  320. */
  321. public static boolean localWriter(Bitmap bm, String name, String path) {
  322. File dataDirectory = Environment.getExternalStorageDirectory();
  323. try {
  324. if (dataDirectory.exists()) {
  325. String s = dataDirectory.getPath() + File.separator + path + File.separator;
  326. File write = new File(s);
  327. if (!write.exists()) {
  328. if (write.mkdirs()) {
  329. FileOutputStream fileOutputStream = new FileOutputStream(new File(s + MD5Util.md5(name)));
  330. fileOutputStream.write(StreamUtils.bitmap2Bytes(bm));
  331. fileOutputStream.close();
  332. }
  333. } else {
  334. FileOutputStream fileOutputStream = new FileOutputStream(new File(s + MD5Util.md5(name)));
  335. fileOutputStream.write(StreamUtils.bitmap2Bytes(bm));
  336. fileOutputStream.close();
  337. }
  338. }
  339. } catch (Exception e) {
  340. return false;
  341. }
  342. return true;
  343. }
  344.  
  345. /**
  346. * Environment.getDataDirectory()
  347. *
  348. * @param bm
  349. * @param name
  350. * @param path
  351. * @return
  352. * @see #localReader(String, String)
  353. */
  354. public static boolean localWriterByPath(Bitmap bm, String name, String path) {
  355. try {
  356. String s = path + File.separator;
  357. File write = new File(s);
  358. if (!write.exists()) {
  359. if (write.mkdirs()) {
  360. FileOutputStream fileOutputStream = new FileOutputStream(new File(s + MD5Util.md5(name)));
  361. fileOutputStream.write(StreamUtils.bitmap2Bytes(bm));
  362. fileOutputStream.close();
  363. }
  364. } else {
  365. FileOutputStream fileOutputStream = new FileOutputStream(new File(s + MD5Util.md5(name)));
  366. fileOutputStream.write(StreamUtils.bitmap2Bytes(bm));
  367. fileOutputStream.close();
  368. }
  369. } catch (Exception e) {
  370. return false;
  371. }
  372. return true;
  373. }
  374. }

工具类总结---(五)---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. 实验楼-2-Linux基础快捷键

    终端:本质上对应着Linux上的/dev/tty设备 shell:打开终端,shell则自动打开 可以在终端直接输入: echo "hello world" /*shell程序自动 ...

  2. jQuery购物车

    效果图 HTML代码:(非表格方式) <div class="nav2"> <input type="checkbox" class=&quo ...

  3. Zabbix3.0部署最佳实践

    Zabbix3整个web界面做了一个全新的设计. 更多新特性请点击当前字幕查看   笔者QQ:572891887 Linux架构交流群:471443208 1.1Zabbix环境准备 [root@li ...

  4. poptest老李谈jvm的GC

    poptest老李谈jvm的GC   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:90882 ...

  5. HBase_在Linux上安装以及运用

    1.上传解压文件 文件:hbase-1.0.1.1-bin.tar 2.更改配置文件 在hbase-env.sh中, export JAVA_HOME=/home/lang/software/jdk1 ...

  6. (iOS)开发中收集的小方法

    1.颜色转变成图片 - (UIImage *)createImageWithColor:(UIColor *)color {     CGRect rect = CGRectMake(0.0f, 0. ...

  7. jquery template.js前端模板引擎

    作为现代应用,ajax的大量使用,使得前端工程师们日常的开发少不了拼装模板,渲染模板 在刚有web的时候,前端与后端的交互,非常直白,浏览器端发出URL,后端返回一张拼好了的HTML串.浏览器对其进行 ...

  8. 理解C++中的头文件和源文件的作用【转】

    一.C++编译模式通常,在一个C++程序中,只包含两类文件--.cpp文件和.h文件.其中,.cpp文件被称作C++源文件,里面放的都是C++的源代码:而.h文件则被称作C++头文件,里面放的也是C+ ...

  9. 【模板】Tarjan求强连通分量

    有人说这篇博客不是很友好,所以我加了点解释,感觉是不是友好多了? dfn[u]表示节点u在dfs时被访问的次序. low[u]表示节点u能够追溯到的最远的祖先的dfn. ins[u]表示节点u是否在栈 ...

  10. .Net MVC4笔记之Razor视图引擎的基础语法

    Razor视图引擎的基础语法: 1.“_”开头的cshtml文档将不能在服务器上访问,和asp.net中的config文档差不多. 2.Razor语法以@开头,以@{}进行包裹. 3.语法使用: 注释 ...