你好,游客 登录 注册 搜索
背景:
阅读新闻

Android数据存储之ContentProvider&Preferences

[日期:2011-12-03] 来源:Linux社区  作者:ajq1989 [字体:大 中 小]

FileOutputStream第二个参数常数系统提供五种.

下面是读取Resource和Assets中的文件的示例.先分别在项目文件下添加文件res/raw/test1.txt和assets/test2.txt用于读取

  1. public class UIData2Activity extends Activity {  
  2.       
  3.     public static final String ENCODING ="UTF-8";  
  4.       
  5.     TextView textView1,textView2;  
  6.       
  7.     /** Called when the activity is first created. */  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.         textView1 =(TextView)findViewById(R.id.tv1);  
  13.         textView2 = (TextView)findViewById(R.id.tv2);  
  14.           
  15.         textView1.setText(getFromRaw("test1.txt"));  
  16.         textView2.setText(getFromAssets("test2.txt"));  
  17.     }  
  18.       
  19.     public String getFromRaw(String fileName){  
  20.         String result ="";  
  21.         try {  
  22.             InputStream stream = getResources().openRawResource(R.raw.test1);  
  23.             int len = stream.available();  
  24.             byte[] bytes = new byte[len];  
  25.             stream.read(bytes);  
  26.               
  27.             result = EncodingUtils.getString(bytes, ENCODING);  
  28.               
  29.             stream.close();  
  30.         } catch (IOException e) {  
  31.             // TODO Auto-generated catch block   
  32.             e.printStackTrace();  
  33.         }  
  34.         return result;  
  35.     }  
  36.       
  37.     public String getFromAssets(String fileName){  
  38.         String result ="";  
  39.         try {  
  40.             InputStream stream = getResources().getAssets().open(fileName);  
  41.             int len = stream.available();  
  42.             byte[] bytes = new byte[len];  
  43.             stream.read(bytes);  
  44.             result = EncodingUtils.getString(bytes, ENCODING);  
  45.             stream.close();  
  46.         } catch (IOException e) {  
  47.             // TODO Auto-generated catch block   
  48.             e.printStackTrace();  
  49.         }  
  50.         return result;  
  51.     }  
  52. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款