Internal Storage用法

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:

  1. Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream.
  2. Write to the file with write().
  3. 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_APPENDMODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE.

To read a file from internal storage:

  1. Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
  2. Read bytes from the file with read().
  3. Then close the stream with close().
  4. =========
  5. fis=openFileInput("某文件名");//打开现成的某文件
  6. buffer=new byte[fis.available()];
  7. fis.read(buffer);//读取该文件内容,将内容送入buffer
  8. ...
  9. ...
  10. 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 Filethat 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

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.

===============================================================================================

这里举个复制图片到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的数据写入新文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值