Android TXT文件读写

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