http://developer.android.com/guide/topics/data/data-storage.html
You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.
To create and write a private file to the internal storage:
- Call
openFileOutput()
with the name of the file and the operating mode. This returns aFileOutputStream
. - Write to the file with
write()
. - Close the stream with
close()
.
For example:
String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);//Filename指新创建的一个文件 fos.write(string.getBytes());//将getBytes的内容存入上面Filename命名的文件中 fos.close();
MODE_PRIVATE
will create the file (or replace a file of the same name) and make it private to your application. Other modes available are: MODE_APPEND
, MODE_WORLD_READABLE
, and MODE_WORLD_WRITEABLE
.
To read a file from internal storage:
- Call
openFileInput()
and pass it the name of the file to read. This returns aFileInputStream
. - Read bytes from the file with
read()
. - Then close the stream with
close()
. - =========
- fis=openFileInput("某文件名");//打开现成的某文件
- buffer=new byte[fis.available()];
- fis.read(buffer);//读取该文件内容,将内容送入buffer
- ...
- ...
- fis.close();
Tip: If you want to save a static file in your application at compile time, save the file in your project res/raw/
directory. You can open it with openRawResource()
, passing the R.raw.<filename>
resource ID. This method returns an InputStream
that you can use to read the file (but you cannot write to the original file).
Saving cache files
If you'd like to cache some data, rather than store it persistently, you should use getCacheDir()
to open a File
that represents the internal directory where your application should save temporary cache files.
When the device is low on internal storage space, Android may delete these cache files to recover space. However, you should not rely on the system to clean up these files for you. You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your application, these files are removed.
Other useful methods
- Gets the absolute path to the filesystem directory where your internal files are saved.
- Creates (or opens an existing) directory within your internal storage space.
- Deletes a file saved on the internal storage.
- Returns an array of files currently saved by your application.
getFilesDir()
getDir()
deleteFile()
fileList()
===============================================================================================
这里举个复制图片到sd卡的例子,是外部存储的实现方法。参考例子13.7
创建新文件————
File file =new File(Environment.getExternalStorageDirectory(),"Background.png")
打开输入流————
InputStream fis =getResources().openRawResource(R.drawable.background);
在try中打开输出流并进行读写————
fos=new FileOutputStream(file);
fis.read(buffer);//从源文件中读取数据放入buffer
fos.write(buffer);//将buffer的数据写入新文件。