文件存储
该文件的存储目录:/data/data/packagename/files/xxx.
1、写入文件数据
通过openFileOutput(String name, int mode)方法获取写入流,通过java的写流进行操作。
FileOutputStream fos
=openFileOutput("name.txt", Context.MODE_PRIVATE);
fos.write("lpl".getBytes());
fos.flush();
这里为了演示,将简单字符串lpl写入到name.txt当中。
打开该文件看看:
2、读取文件数据
通过openFileInput获取写入的文件,通过java读流进行操作。
FileInputStream fis=openFileInput("name.txt");
byte b[] =new byte[10];
int len=0;
StringBuffer buffer=new StringBuffer();
while((len=fis.read(b))!=-1)
{
buffer.append(new String(b,0,len));
}
Toast.makeText(getApplicationContext(), buffer, 0).show();
不要忘了记得关闭流。
由于有java的io流,所以要记得抛异常。