数据存储在文件中 默认情况下,文件是不能在不同程序之间共享的。 存储数据 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对象,直接读取文件内容。 |