一、私有文件夹下的文件存取(/data/data/包名)

Java代码
 
 1 import java.io.FileInputStream;  
 2 import java.io.FileOutputStream;  
 3 import org.apache.http.util.EncodingUtils;  
 4   
 5 public void writeFileData(String fileName,String message){  
 6     try{  
 7      FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);  
 8      byte [] bytes = message.getBytes();  
 9      fout.write(bytes);  
10       fout.close();  
11      }  
12     catch(Exception e){  
13      e.printStackTrace();  
14     }  
15 }     
16   
17   
18 public String readFileData(String fileName){  
19      String res="";  
20      try{  
21       FileInputStream fin = openFileInput(fileName);  
22       int length = fin.available();  
23       byte [] buffer = new byte[length];  
24       fin.read(buffer);      
25       res = EncodingUtils.getString(buffer, "UTF-8");  
26       fin.close();      
27      }  
28      catch(Exception e){  
29       e.printStackTrace();  
30      }  
31      return res;  
32     }    

 


 

二、从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写)
   

Java代码
 
 1 public String getFromRaw(String fileName){  
 2     String res = "";  
 3        try{  
 4         InputStream in = getResources().openRawResource(R.raw.test1);   
 5         int length = in.available();        
 6         byte [] buffer = new byte[length];         
 7         in.read(buffer);          
 8         res = EncodingUtils.getString(buffer, "UTF-8");     
 9         in.close();             
10        }  
11        catch(Exception e){  
12         e.printStackTrace();          
13        }  
14     return res ;  
15    }  

 


三、从asset中获取文件并读取数据(资源文件只能读不能写)
   
Java代码
 
 1 public String getFromAsset(String fileName){  
 2     String res="";  
 3     try{  
 4      InputStream in = getResources().getAssets().open(fileName);    
 5      int length = in.available();          
 6         byte [] buffer = new byte[length];         
 7         in.read(buffer);             
 8         res = EncodingUtils.getString(buffer, "UTF-8");      
 9     }  
10     catch(Exception e){  
11      e.printStackTrace();          
12     }  
13     return res;  
14    }  

 

 

posted on 2012-07-18 15:41  清沁  阅读(355)  评论(0编辑  收藏  举报