歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android入門:實現一個File存儲的輔助類

Android入門:實現一個File存儲的輔助類

日期:2017/3/1 10:15:58   编辑:Linux編程

Android入門:File文件存儲 教程鏈接:http://www.linuxidc.com/Linux/2012-07/66002.htm

  1. package com.xiazdong.file.util;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import android.content.Context;
  9. import android.os.Environment;
  10. public class FileUtil {
  11. /**
  12. * 保存文本到內存
  13. * @param context
  14. * @param filename
  15. * @param content
  16. * @param mode
  17. * @throws Exception
  18. */
  19. public static void saveTextInMemory(Context context,String filename,String content,int mode) throws Exception{
  20. try{
  21. FileOutputStream out = context.openFileOutput(filename, mode);
  22. out.write(content.getBytes("UTF-8"));
  23. out.close();
  24. }
  25. catch(Exception e){
  26. throw new Exception();
  27. }
  28. }
  29. /**
  30. * 保存文件到sdcard
  31. * @param filename
  32. * @param content
  33. * @throws Exception
  34. */
  35. public static void saveTextInSdcard(String filename,String content) throws Exception{
  36. try{
  37. File f = new File(Environment.getExternalStorageDirectory(),filename);
  38. FileOutputStream out = new FileOutputStream(f);
  39. out.write(content.getBytes("UTF-8"));
  40. out.close();
  41. }
  42. catch(Exception e){
  43. throw new Exception();
  44. }
  45. }
  46. /**
  47. * 從內存讀取文件
  48. * @param filename
  49. * @return
  50. * @throws Exception
  51. */
  52. public static String loadTextFromSdcard(String filename) throws Exception{
  53. try{
  54. File f = new File(Environment.getExternalStorageDirectory(),filename);
  55. FileInputStream in = new FileInputStream(f);
  56. byte[]data = read2byte(in);
  57. return new String(data,"UTF-8");
  58. }
  59. catch(Exception e){
  60. throw new Exception();
  61. }
  62. }
  63. /**
  64. * 從sdcard讀取文件
  65. * @param context
  66. * @param filename
  67. * @return
  68. * @throws Exception
  69. */
  70. public static String loadTextFromMemory(Context context,String filename) throws Exception{
  71. try{
  72. FileInputStream in = context.openFileInput(filename);
  73. byte[]data = read2byte(in);
  74. return new String(data,"UTF-8");
  75. }
  76. catch(Exception e){
  77. throw new Exception();
  78. }
  79. }
  80. private static byte[] read2byte(InputStream in) throws IOException {
  81. byte[] data;
  82. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  83. byte[]buf = new byte[1024];
  84. int len = 0;
  85. while((len = in.read(buf))!=-1){
  86. bout.write(buf, 0, len);
  87. }
  88. data = bout.toByteArray();
  89. return data;
  90. }
  91. }

測試代碼:

  1. FileUtil.saveTextInSdcard("1.txt","hello"); //將"hello"保存到/mnt/sdcard/1.txt中
  2. String content = FileUtil.loadTextFromSdcard("1.txt"); //讀取/mnt/sdcard/1.txt內容
  3. FileUtil.saveTextInMemory(MainActivity.this,"1.txt","hello", Context.MODE_PRIVATE); //將hello保存到/data/data/package/files/1.txt中
  4. String content = FileUtil.loadTextFromMemory(MainActivity.this, "1.txt"); //讀取/data/data/package/files/1.txt內容
Copyright © Linux教程網 All Rights Reserved