Android TXT文件读写

时间:2023-03-09 16:33:04
Android TXT文件读写
  1. package com.wirelessqa.helper;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.InputStream;
  5. import org.apache.http.util.EncodingUtils;
  6. import android.app.Activity;
  7. public class FileAccess extends Activity {
  8. /**
  9. * 一、私有文件夹下的文件存取(/data/data/包名/files)
  10. *
  11. * @param fileName
  12. * @param message
  13. */
  14. public void writeFileData(String fileName, String message) {
  15. try {
  16. FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
  17. byte[] bytes = message.getBytes();
  18. fout.write(bytes);
  19. fout.close();
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. /**
  25. * //读文件在./data/data/包名/files/下面
  26. *
  27. * @param fileName
  28. * @return
  29. */
  30. public String readFileData(String fileName) {
  31. String res = "";
  32. try {
  33. FileInputStream fin = openFileInput(fileName);
  34. int length = fin.available();
  35. byte[] buffer = new byte[length];
  36. fin.read(buffer);
  37. res = EncodingUtils.getString(buffer, "UTF-8");
  38. fin.close();
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. return res;
  43. }
  44. /**
  45. * 写, 读sdcard目录上的文件,要用FileOutputStream, 不能用openFileOutput
  46. * 不同点:openFileOutput是在raw里编译过的,FileOutputStream是任何文件都可以
  47. * @param fileName
  48. * @param message
  49. */
  50. // 写在/mnt/sdcard/目录下面的文件
  51. public void writeFileSdcard(String fileName, String message) {
  52. try {
  53. // FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
  54. FileOutputStream fout = new FileOutputStream(fileName);
  55. byte[] bytes = message.getBytes();
  56. fout.write(bytes);
  57. fout.close();
  58. }
  59. catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. // 读在/mnt/sdcard/目录下面的文件
  64. public String readFileSdcard(String fileName) {
  65. String res = "";
  66. try {
  67. FileInputStream fin = new FileInputStream(fileName);
  68. int length = fin.available();
  69. byte[] buffer = new byte[length];
  70. fin.read(buffer);
  71. res = EncodingUtils.getString(buffer, "UTF-8");
  72. fin.close();
  73. }
  74. catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. return res;
  78. }
  79. /**
  80. * 二、从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写)
  81. *
  82. * @param fileInRaw
  83. * @return
  84. */
  85. public String readFromRaw(int fileInRaw) {
  86. String res = "";
  87. try {
  88. InputStream in = getResources().openRawResource(fileInRaw);
  89. int length = in.available();
  90. byte[] buffer = new byte[length];
  91. in.read(buffer);
  92. res = EncodingUtils.getString(buffer, "GBK");
  93. // res = new String(buffer,"GBK");
  94. in.close();
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98. return res;
  99. }
  100. /**
  101. * 三、从asset中获取文件并读取数据(资源文件只能读不能写)
  102. *
  103. * @param fileName
  104. * @return
  105. */
  106. public String readFromAsset(String fileName) {
  107. String res = "";
  108. try {
  109. InputStream in = getResources().getAssets().open(fileName);
  110. int length = in.available();
  111. byte[] buffer = new byte[length];
  112. in.read(buffer);
  113. res = EncodingUtils.getString(buffer, "UTF-8");
  114. } catch (Exception e) {
  115. e.printStackTrace();
  116. }
  117. return res;
  118. }
  119. }