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

下面是读取Resource和Assets中的文件的示例.先分别在项目文件下添加文件res/raw/test1.txt和assets/test2.txt用于读取
- public class UIData2Activity extends Activity {
- public static final String ENCODING ="UTF-8";
- TextView textView1,textView2;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- textView1 =(TextView)findViewById(R.id.tv1);
- textView2 = (TextView)findViewById(R.id.tv2);
- textView1.setText(getFromRaw("test1.txt"));
- textView2.setText(getFromAssets("test2.txt"));
- }
- public String getFromRaw(String fileName){
- String result ="";
- try {
- InputStream stream = getResources().openRawResource(R.raw.test1);
- int len = stream.available();
- byte[] bytes = new byte[len];
- stream.read(bytes);
- result = EncodingUtils.getString(bytes, ENCODING);
- stream.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return result;
- }
- public String getFromAssets(String fileName){
- String result ="";
- try {
- InputStream stream = getResources().getAssets().open(fileName);
- int len = stream.available();
- byte[] bytes = new byte[len];
- stream.read(bytes);
- result = EncodingUtils.getString(bytes, ENCODING);
- stream.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return result;
- }
- }
