源码教程 2025年06月7日
0 收藏 0 点赞 407 浏览 5007 个字
摘要 :

Android的文件存储方式分为两种:内部存储和外部存储。 1) 内部存储 内部存储是指将应用程序的数据以文件方式存储到设备内存中。以内部存储方式存储的文件属于其所创建的……

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 所示。

Android数据存储之文件存储使用

图 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. <TextView

  7. android:layout_width="fill_parent"

  8. android:layout_height="wrap_content"

  9. android:text="使用文件存储程序信息" />

  10. <TextView

  11. android:layout_width="fill_parent"

  12. android:layout_height="wrap_content"

  13. android:text="输出您存入的信息" />

  14. <EditText

  15. android:id="@+id/phone_text"

  16. android:layout_width="fill_parent"

  17. android:layout_height="wrap_content"

  18. android:hint="输入保存的信息" />

  19. <LinearLayout

  20. android:layout_width="wrap_content"

  21. android:layout_height="wrap_content"

  22. android:orientation="vertical">

  23. <Button

  24. android:id="@+id/SaveButton"

  25. android:layout_width="wrap_content"

  26. android:layout_height="wrap_content"

  27. android:text="保存信息" />

  28. <Button

  29. android:id="@+id/LoadButton"

  30. android:layout_width="wrap_content"

  31. android:layout_height="wrap_content"

  32. android:text="读取信息" />

  33. </LinearLayout>

  34. </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. <uses-sdk android:minSdkVersion="14" />

  7. <application

  8. android:allowBackup="true"

  9. android:icon="@mipmap/ic_launcher"

  10. android:label="@string/app_name"

  11. android:roundIcon="@mipmap/ic_launcher_round"

  12. android:supportsRtl="true"

  13. android:theme="@style/AppTheme">

  14. <activity android:name=".MainActivity">

  15. <intent-filter>

  16. <action android:name="android.intent.action.MAIN" />

  17. <category android:name="android.intent.category.LAUNCHER" />

  18. </intent-filter>

  19. </activity>

  20. </application>

  21. </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. import java.io.FileInputStream;

  8. import java.io.FileOutputStream;

  9. public class MainActivity extends Activity {

  10. private EditText SaveText;

  11. private Button SaveButton, LoadButton;

  12. @Override

  13. public void onCreate(Bundle savedInstanceState) {

  14. super.onCreate(savedInstanceState);

  15. setContentView(R.layout.activity_main);

  16. SaveText = (EditText) findViewById(R.id.phone_text);

  17. SaveButton = (Button) findViewById(R.id.SaveButton);

  18. LoadButton = (Button) findViewById(R.id.LoadButton);

  19. SaveButton.setOnClickListener(new ButtonListener());

  20. LoadButton.setOnClickListener(new ButtonListener());

  21. }

  22. private class ButtonListener implements View.OnClickListener {

  23. @Override

  24. public void onClick(View v) {

  25. switch (v.getId()) {

  26. /*保存数据*/

  27. case R.id.SaveButton:

  28. String saveinfo = SaveText.getText().toString().trim();

  29. FileOutputStream fos;

  30. try {

  31. fos = openFileOutput("text", MODE_APPEND);

  32. fos.write(saveinfo.getBytes());

  33. fos.close();

  34. } catch (Exception e) {

  35. e.printStackTrace();

  36. }

  37. Toast.makeText(MainActivity.this, "数据保存成功", Toast.LENGTH_LONG).

  38. show();

  39. break;

  40. /*读取数据*/

  41. case R.id.LoadButton:

  42. String get = "";

  43. try {

  44. FileInputStream fis = openFileInput("text");

  45. byte[] buffer = new byte[fis.available()];

  46. fis.read(buffer);

  47. get = new String(buffer);

  48. } catch (Exception e) {

  49. e.printStackTrace();

  50. }

  51. Toast.makeText(MainActivity.this, "保存的数据是" + get,

  52. Toast.LENGTH_LONG).show();

  53. break;

  54. default:

  55. break;

  56. }

  57. }

  58. }

  59. }

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/1057.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

701
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

715
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

307
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,039
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

835
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

723
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号