文件数据存储

数据存储在文件中

默认情况下,文件是不能在不同程序之间共享的。

存储数据

void
 save
()
 
throws
 
Exception
{

   
Properties properties = new Properties ();
   
//将数据打包成Properties
   properties
. put ( "tag" , String . valueOf ( false ));
   
   
FileOutputStream stream = this . openFileOutput ( "file.cfg" , Context . MODE_WORLD_WRITEABLE );
   
//将打包好的数据写入文件中
   properties
. stroe ( stream , "" );
}

读取数据

void
 load
()
 
throws
 
Exception
{

   
// 构建Properties对象
   
Properties properties = new Properties ();
   
//读取文件(如果文件不存在就自动创建文件)
   
FileInputStream stream = this . openFileInput ( "file.cfg" );
   
//读取文件内容
    properties
. load ( stream );
   
   
boolean tag = Boolean . valueOf ( properties . get ( "tag" ). toString ());
}

查看文件

data/data/项目文件夹/files/file.cfg

初始化程序加载文件

在res/raw/tempFile中建立一个static文件,在程序中通过Resources.openRawResource(R.raw.文件名) 方法返回一个InputStream对象,直接读取文件内容。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值