解析txt文件数据

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