橘子味的心
标题:7.2 Android文件存储

Android的文件存储方式分为两种:内部存储和外部存储。

1) 内部存储

内部存储是指将应用程序的数据以文件方式存储到设备内存中。以内部存储方式存储的文件属于其所创建的应用程序私有,其他应用程序无权进行操作。

当创建的应用程序被卸载时,其内部存储的文件也随之被删除。当内部存储器的存储空间不足时,缓存文件可能会被删除以释放空间。

因此,缓存文件是不可靠的。当使用缓存文件时,自己应该维护好缓存文件,并且将缓存文件限制在特定大小之内。

使用文件存储信息时,使用 openFileOutput 和 openFileInput 进行文件的读写,这跟 Java 中的 I/O 程序很类似。创建并写内部存储文件的步骤如下:

1)通过 Context.openFileOutput(String name, int mode) 方法打开文件并设定读写方式,返回 FileOutputStream。

其中,参数 mode 取值为:
  • MODE_PRIVATE:默认访问方式,文件仅能被创建应用程序访问。
  • MODE_APPEND:若文件已经存在,则在文件末尾继续写入数据,而不抹掉文件原有内容。
  • MODE_WORLD_READABLE:允许该文件被其他应用程序执行读取内容操作。
  • MODE_WORLD_WRITEABLE:允许该文件被其他应用程序执行写操作。
2) 调用 FileOutputStream.write() 方法写入数据。

3) 调用 FileOutputStream.close() 方法关闭输出流,完成写操作。

内部存储文件的写文件示例代码如下:
  1. String FILENAME="hello_file";
  2. String string="hello world";
  3. FileOutputStream fos = openFileOutput(FILENAME,Context.MODE_PRIVATE);
  4. fos.write(string.getBytes());
  5. fos.close();

2) 外部存储

外部存储是指将文件存储到一些外部存储设备上。例如 SD 卡或者设备内嵌的存储卡,属于永久性的存储方式。

外部存储的文件不被某个应用程序所特有,可以被其他应用程序共享,当将该外部存储设备连接到计算机上时,这些文件可以被浏览、修改和删除。因此,这种存储方式不具有安全性。

由于外部存储器可能处于被移除、连接到计算机、丢失、只读或者其他各种状态,因此在使用外部存储之前,必须使用 Environment.getExternalStorageState() 方法来确认外部存储器是否可用。

验证外部存储器是否可读写的代码如下:
  1. boolean mExternalStorageAvailable=false;
  2. boolean mExternalStorageWriteable=false;
  3. String state = Environment.getExternalStorageState();
  4. if(Environment.MEDIA_MOUNTED.equals(state)){
  5. //外部存储器可读写
  6. mExternalStorageAvailable = mExternalStorageWriteable = true;
  7. }else if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
  8. //外部存储器可读不可写
  9. mExternalStorageAvailable=true;
  10. mExternalStorageWriteable=false;
  11. }else{
  12. //外部存储器不可读写,处于其他状态
  13. mExternalStorageAvailable = mExternalStorageWriteable = false;
  14. }
此外,在程序开发过程中还可以使用缓存文件(Cache),内部存储和外部存储都可以用于保存缓存文件。

如上述一样,当存储器的存储空间不足时,缓存文件可能会被删除以释放空间。因此,缓存文件是不可靠的。当使用缓存文件时,应该自己维护好缓存文件,并且将缓存文件限制在特定大小之内。

使用文件存储功能

实例 FileDemo 演示了使用文件存储的功能,其运行效果如图 1 所示。

FileDemo运行结果
图 1  FileDemo运行结果

该实例将文本框中输入的内容存储到名为 text 的文件中。当该应用程序再次启动时,可以从 text 文件写入的内容中读取并显示出来。

本实例使用内部存储方式,我们可以在 data/data/<your package name>/files 目录下找到名为 text 的文件。

本实例没有将文件放置到 SD卡 中,可自行实现将文件保存在 SD 卡中的操作。

实例 FileDemo 的布局文件 main.xml 中放置了两个 TextView、一个 EditText 和两个 Button,其代码如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6.  
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="使用文件存储程序信息" />
  11.  
  12. <TextView
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:text="输出您存入的信息" />
  16.  
  17. <EditText
  18. android:id="@+id/phone_text"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:hint="输入保存的信息" />
  22.  
  23. <LinearLayout
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:orientation="vertical">
  27.  
  28. <Button
  29. android:id="@+id/SaveButton"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:text="保存信息" />
  33.  
  34. <Button
  35. android:id="@+id/LoadButton"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:text="读取信息" />
  39.  
  40. </LinearLayout>
  41.  
  42. </LinearLayout>
实例 FileDemo 中 AndroidManifest.xml 文件的代码如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="introduction.android.fileDemo"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6.  
  7. <uses-sdk android:minSdkVersion="14" />
  8. <application
  9. android:allowBackup="true"
  10. android:icon="@mipmap/ic_launcher"
  11. android:label="@string/app_name"
  12. android:roundIcon="@mipmap/ic_launcher_round"
  13. android:supportsRtl="true"
  14. android:theme="@style/AppTheme">
  15. <activity android:name=".MainActivity">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23.  
  24. </manifest>
实例 FileDemo 中 MainActivity.java 的代码如下:
  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.Button;
  5. import android.widget.EditText;
  6. import android.widget.Toast;
  7.  
  8. import java.io.FileInputStream;
  9. import java.io.FileOutputStream;
  10.  
  11. public class MainActivity extends Activity {
  12. private EditText SaveText;
  13. private Button SaveButton, LoadButton;
  14.  
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. SaveText = (EditText) findViewById(R.id.phone_text);
  20. SaveButton = (Button) findViewById(R.id.SaveButton);
  21. LoadButton = (Button) findViewById(R.id.LoadButton);
  22. SaveButton.setOnClickListener(new ButtonListener());
  23. LoadButton.setOnClickListener(new ButtonListener());
  24. }
  25.  
  26. private class ButtonListener implements View.OnClickListener {
  27. @Override
  28. public void onClick(View v) {
  29. switch (v.getId()) {
  30. /*保存数据*/
  31. case R.id.SaveButton:
  32. String saveinfo = SaveText.getText().toString().trim();
  33. FileOutputStream fos;
  34. try {
  35. fos = openFileOutput("text", MODE_APPEND);
  36. fos.write(saveinfo.getBytes());
  37. fos.close();
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. Toast.makeText(MainActivity.this, "数据保存成功", Toast.LENGTH_LONG).
  42. show();
  43. break;
  44. /*读取数据*/
  45. case R.id.LoadButton:
  46. String get = "";
  47. try {
  48. FileInputStream fis = openFileInput("text");
  49. byte[] buffer = new byte[fis.available()];
  50. fis.read(buffer);
  51. get = new String(buffer);
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55.  
  56. Toast.makeText(MainActivity.this, "保存的数据是" + get,
  57. Toast.LENGTH_LONG).show();
  58. break;
  59. default:
  60. break;
  61.  
  62. }
  63. }
  64. }
  65. }