Android file IO

源地址:http://blog.csdn.net/ajq1989/article/details/6899343

 Android平台下的数据存储主要包括文件的流读取,轻量级数据库SQLite,ContentProvider和Preference

            当App被安装后.其所在的安装包中会有一个相应的文件夹用于存放自己的数据.只有应用程序自己本身才对这个文件夹有写入权限,路径是/data/data/APP包名/.下面是使用文件I/O方法直接往手机中存储数据.主要使用了FileInputStream和FileOutputStream这个两个类.


  1. public class UIDataActivity extends Activity {
  2.     public static final String ENCODING = “UTF-8”;
  3.     String fileName=”test.txt”;
  4.     String message = “Android数据存储I/O例子 “;
  5.     TextView textView;
  6.     /** Called when the activity is first created. */
  7.     @Override
  8.     public void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.main);
  11.         /*
  12.          * 本示例是应用程序在私有数据文件夹下创建一个文件并读取其中的数据显示在TextView上
  13.          */
  14.         writeFileData(fileName,message);
  15.         String result = readFileData(fileName);
  16.         textView = (TextView)findViewById(R.id.tv);
  17.         textView.setText(result);
  18.     }
  19.     public void writeFileData(String fileName,String message){
  20.         //使用FileOutputStream对象如果文件不存在或者不可以写入时.会抛出FileNotFoundException异常
  21.         try {
  22.             FileOutputStream stream = openFileOutput(fileName, MODE_PRIVATE);
  23.             byte[] bytes = message.getBytes();
  24.             stream.write(bytes);
  25.             stream.close();
  26.         } catch (FileNotFoundException e) {
  27.             // TODO Auto-generated catch block
  28.             e.printStackTrace();
  29.         } catch (IOException e) {
  30.             // TODO Auto-generated catch block
  31.             e.printStackTrace();
  32.         }
  33.     }
  34.     public String readFileData(String fileName){
  35.         String result =””;
  36.         try {
  37.             FileInputStream stream = openFileInput(fileName);
  38.             int len = stream.available();
  39.             byte[] bytes = new byte[len];
  40.             stream.read(bytes);
  41.             result = EncodingUtils.getString(bytes, ENCODING);
  42.             stream.close();
  43.         } catch (FileNotFoundException e) {
  44.             // TODO Auto-generated catch block
  45.             e.printStackTrace();
  46.         } catch (IOException e) {
  47.             // TODO Auto-generated catch block
  48.             e.printStackTrace();
  49.         }
  50.         return result;
  51.     }
  52. }

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

更多关于Context对象API操作点这

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


  1. public class UIData2Activity extends Activity {
  2.     public static final String ENCODING =”UTF-8″;
  3.     TextView textView1,textView2;
  4.     /** Called when the activity is first created. */
  5.     @Override
  6.     public void onCreate(Bundle savedInstanceState) {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.main);
  9.         textView1 =(TextView)findViewById(R.id.tv1);
  10.         textView2 = (TextView)findViewById(R.id.tv2);
  11.         textView1.setText(getFromRaw(“test1.txt”));
  12.         textView2.setText(getFromAssets(“test2.txt”));
  13.     }
  14.     public String getFromRaw(String fileName){
  15.         String result =””;
  16.         try {
  17.             InputStream stream = getResources().openRawResource(R.raw.test1);
  18.             int len = stream.available();
  19.             byte[] bytes = new byte[len];
  20.             stream.read(bytes);
  21.             result = EncodingUtils.getString(bytes, ENCODING);
  22.             stream.close();
  23.         } catch (IOException e) {
  24.             // TODO Auto-generated catch block
  25.             e.printStackTrace();
  26.         }
  27.         return result;
  28.     }
  29.     public String getFromAssets(String fileName){
  30.         String result =””;
  31.         try {
  32.             InputStream stream = getResources().getAssets().open(fileName);
  33.             int len = stream.available();
  34.             byte[] bytes = new byte[len];
  35.             stream.read(bytes);
  36.             result = EncodingUtils.getString(bytes, ENCODING);
  37.             stream.close();
  38.         } catch (IOException e) {
  39.             // TODO Auto-generated catch block
  40.             e.printStackTrace();
  41.         }
  42.         return result;
  43.     }
  44. }
Advertisements
This entry was posted in Android. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s