Android数据存储之文件I/O

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

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

public class UIDataActivity extends Activity { public static final String ENCODING = "UTF-8"; String fileName="test.txt"; String message = "Android数据存储I/O例子 "; TextView textView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /* * 本示例是应用程序在私有数据文件夹下创建一个文件并读取其中的数据显示在TextView上 */ writeFileData(fileName,message); String result = readFileData(fileName); textView = (TextView)findViewById(R.id.tv); textView.setText(result); } public void writeFileData(String fileName,String message){ //使用FileOutputStream对象如果文件不存在或者不可以写入时.会抛出FileNotFoundException异常 try { FileOutputStream stream = openFileOutput(fileName, MODE_PRIVATE); byte[] bytes = message.getBytes(); stream.write(bytes); stream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String readFileData(String fileName){ String result =""; try { FileInputStream stream = openFileInput(fileName); int len = stream.available(); byte[] bytes = new byte[len]; stream.read(bytes); result = EncodingUtils.getString(bytes, ENCODING); stream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } }


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

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

下面是读取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; } }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值