4.6.1.1 使用私有文件

这种情况下使用的文件,只能在同一个应用中读取/写入,并且这是使用文件的一种非常安全的方式。 原则上,无论存储在文件中的信息是否是公开的,尽可能使用私有文件,当与其他应用交换必要的信息时,应该使用另一个 Android 系统(内容供应器,服务)来完成。

要点:

  1. 文件必须在应用目录中创建。

  2. 文件的访问权限必须设置为私有模式,以免其他应用使用。

  3. 可以存储敏感信息。

  4. 对于存储在文件中的信息,请仔细和安全地处理文件数据。

PrivateFileActivity.java

  1. package org.jssec.android.file.privatefile;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import android.app.Activity;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.TextView;
  11. public class PrivateFileActivity extends Activity {
  12. private TextView mFileView;
  13. private static final String FILE_NAME = "private_file.dat";
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.file);
  18. mFileView = (TextView) findViewById(R.id.file_view);
  19. }
  20. /**
  21. * Create file process
  22. *
  23. * @param view
  24. */
  25. public void onCreateFileClick(View view) {
  26. FileOutputStream fos = null;
  27. try {
  28. // *** POINT 1 *** Files must be created in application directory.
  29. // *** POINT 2 *** The access privilege of file must be set private mode in order not to be used by other applications.
  30. fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
  31. // *** POINT 3 *** Sensitive information can be stored.
  32. // *** POINT 4 *** Regarding the information to be stored in files, handle file data carefully and securely.
  33. // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."
  34. fos.write(new String("Not sensotive information (File Activity)¥n").getBytes());
  35. } catch (FileNotFoundException e) {
  36. mFileView.setText(R.string.file_view);
  37. } catch (IOException e) {
  38. android.util.Log.e("PrivateFileActivity", "failed to read file");
  39. } finally {
  40. if (fos != null) {
  41. try {
  42. fos.close();
  43. } catch (IOException e) {
  44. android.util.Log.e("PrivateFileActivity", "failed to close file");
  45. }
  46. }
  47. }
  48. finish();
  49. }
  50. /**
  51. * Read file process
  52. *
  53. * @param view
  54. */
  55. public void onReadFileClick(View view) {
  56. FileInputStream fis = null;
  57. try {
  58. fis = openFileInput(FILE_NAME);
  59. byte[] data = new byte[(int) fis.getChannel().size()];
  60. fis.read(data);
  61. String str = new String(data);
  62. mFileView.setText(str);
  63. } catch (FileNotFoundException e) {
  64. mFileView.setText(R.string.file_view);
  65. } catch (IOException e) {
  66. android.util.Log.e("PrivateFileActivity", "failed to read file");
  67. } finally {
  68. if (fis != null) {
  69. try {
  70. fis.close();
  71. } catch (IOException e) {
  72. android.util.Log.e("PrivateFileActivity", "failed to close file");
  73. }
  74. }
  75. }
  76. }
  77. /**
  78. * Delete file process
  79. *
  80. * @param view
  81. */
  82. public void onDeleteFileClick(View view) {
  83. File file = new File(this.getFilesDir() + "/" + FILE_NAME);
  84. file.delete();
  85. mFileView.setText(R.string.file_view);
  86. }
  87. }

PrivateUserActivity.java

  1. package org.jssec.android.file.privatefile;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.TextView;
  11. public class PrivateUserActivity extends Activity {
  12. private TextView mFileView;
  13. private static final String FILE_NAME = "private_file.dat";
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.user);
  18. mFileView = (TextView) findViewById(R.id.file_view);
  19. }
  20. private void callFileActivity() {
  21. Intent intent = new Intent();
  22. intent.setClass(this, PrivateFileActivity.class);
  23. startActivity(intent);
  24. }
  25. /**
  26. * Call file Activity process
  27. *
  28. * @param view
  29. */
  30. public void onCallFileActivityClick(View view) {
  31. callFileActivity();
  32. }
  33. /**
  34. * Read file process
  35. *
  36. * @param view
  37. */
  38. public void onReadFileClick(View view) {
  39. FileInputStream fis = null;
  40. try {
  41. fis = openFileInput(FILE_NAME);
  42. byte[] data = new byte[(int) fis.getChannel().size()];
  43. fis.read(data);
  44. // *** POINT 4 *** Regarding the information to be stored in files, handle file data carefully and securely.
  45. // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."
  46. String str = new String(data);
  47. mFileView.setText(str);
  48. } catch (FileNotFoundException e) {
  49. mFileView.setText(R.string.file_view);
  50. } catch (IOException e) {
  51. android.util.Log.e("PrivateUserActivity", "failed to read file");
  52. } finally {
  53. if (fis != null) {
  54. try {
  55. fis.close();
  56. } catch (IOException e) {
  57. android.util.Log.e("PrivateUserActivity", "failed to close file");
  58. }
  59. }
  60. }
  61. }
  62. /**
  63. * Rewrite file process
  64. *
  65. * @param view
  66. */
  67. public void onWriteFileClick(View view) {
  68. FileOutputStream fos = null;
  69. try {
  70. // *** POINT 1 *** Files must be created in application directory.
  71. // *** POINT 2 *** The access privilege of file must be set private mode in order not to be used by other applications.
  72. fos = openFileOutput(FILE_NAME, MODE_APPEND);
  73. // *** POINT 3 *** Sensitive information can be stored.
  74. // *** POINT 4 *** Regarding the information to be stored in files, handle file data carefully and securely.
  75. // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."
  76. fos.write(new String("Sensitive information (User Activity)¥n").getBytes());
  77. } catch (FileNotFoundException e) {
  78. mFileView.setText(R.string.file_view);
  79. } catch (IOException e) {
  80. android.util.Log.e("PrivateUserActivity", "failed to read file");
  81. } finally {
  82. if (fos != null) {
  83. try {
  84. fos.close();
  85. } catch (IOException e) {
  86. android.util.Log.e("PrivateUserActivity", "failed to close file");
  87. }
  88. }
  89. }
  90. callFileActivity();
  91. }
  92. }