Internal Storage
可以在设备的内存中保存文件。这些文件不能被其他应用访问(基于不能被用户访问)。当卸载应用时,这些文件会被删除。
写文件
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
openFileOutput()第二个参数有下面4个值:
- MODE_PRIVATE 会创建一个新文件,如果原文件已存在,会替代原文件。
- MODE_APPEND 如果文件已存在,会使用存在的文件,否则创新一个新文件。
- MODE_WORLD_READABLE 不再推荐使用,文件可被其他应用访问???
- MODE_WORLD_WRITEABLE 不再推荐使用,文件可被其他应用写???
读文件
读内存中的文件使用openFileInput()方法 读res/raw/下的文件,可使用openRawResource()
保存缓存文件
getCacheDir() 返回的File,在内置不够时,会被系统自动清除。但是请不要依赖系统的清除机制。 应该通过程序控制缓存文件,并限制缓存文件的大小。
其他方法
- getFilesDir() Gets the absolute path to the filesystem directory where your internal files are saved.
- getDir() Creates (or opens an existing) directory within your internal storage space.
- deleteFile() Deletes a file saved on the internal storage.
- fileList() Returns an array of files currently saved by your application.