服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android 数据存储方式有哪几种

Android 数据存储方式有哪几种

2021-04-08 15:48doodle777 Android

android为数据存储提供了五种方式,有SharedPreferences、文件存储、SQLite数据库、ContentProvider、网络存储,对android数据存储方式感兴趣的朋友可以通过本文学习一下

以下内容给大家介绍Android数据存储提供了五种方式:

1、SharedPreferences

2、文件存储

3、SQLite数据库

4、ContentProvider

5、网络存储

  本文主要介绍如何使用文件来存储数据。Android文件操作用到的是Java.IO中的FileOutputStream和FileInputStream类。

一、存储文件

  首先实例化一个FileOutputStream。

FileOutputStream foStream = openFileOutput(fileName, MODE_PRIVATE);
// fileName: 要写入文件的名称
// MODE_PRIVATE: 为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
// MODE_APPEND: 模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
// MODE_WORLD_READABLE: 表示当前文件可以被其他应用读取,不推荐使用
// MODE_WORLD_WRITEABLE: 表示当前文件可以被其他应用写入,不推荐使用

  然后调用foStream.write()即可完成写入。

byte[] buffer = fileContent.getBytes();
foStream.write(buffer);
Toast.makeText(MainActivity.this, "写入成功",Toast.LENGTH_SHORT).show();

  最后进行一些清理工作,刷新写出流和关闭流。

foStream.flush();
foStream.close();

二、读取文件

  同样的,首先实例化一个FileInputStream。

FileInputStream fiStream = openFileInput(fileName)

  然后调用fiStream.read()即可

int len = fiStream.available();
byte[] buffer = new byte[len];
fiStream.read(buffer)

  最后,将文本显示并关闭读文件流

etContent.setText(new String(buffer));
Toast.makeText(MainActivity.this, "读取成功",Toast.LENGTH_SHORT).show();
fiStream.close();

三、完整代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import android.support.v.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Toast;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 public class MainActivity extends AppCompatActivity {
   private EditText etName;
   private EditText etContent;
   private Button btnWrite;
   private Button btnRead;
   private String fileName = "";
   private String fileContent = "";
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     etName = (EditText)findViewById(R.id.etName);
     etContent = (EditText)findViewById(R.id.etContent);
     btnWrite = (Button)findViewById(R.id.btnWrite);
     btnRead = (Button)findViewById(R.id.btnRead);
     btnWrite.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
         fileName = etName.getText().toString();
         fileContent = etContent.getText().toString();
         try {
           FileOutputStream foStream = openFileOutput(fileName, MODE_PRIVATE);
           byte[] buffer = fileContent.getBytes();
           foStream.write(buffer);
           Toast.makeText(MainActivity.this, "写入成功",Toast.LENGTH_SHORT).show();
           foStream.flush();
           foStream.close();
         }catch(Exception e){
           e.printStackTrace();
         }
       }
     });
     btnRead.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
         fileName = etName.getText().toString();
         try{
           FileInputStream fiStream = openFileInput(fileName);
           int len = fiStream.available();
           byte[] buffer = new byte[len];
           fiStream.read(buffer);
           etContent.setText(new String(buffer));
           Toast.makeText(MainActivity.this, "读取成功",Toast.LENGTH_SHORT).show();
           fiStream.close();
         }catch(Exception e){
           e.printStackTrace();
         }
       }
     });
   }
 }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   <EditText
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/etName"
     android:layout_alignParentTop="true"
     android:layout_alignParentLeft="true"
     android:layout_alignParentStart="true"
     android:layout_alignParentRight="true"
     android:layout_alignParentEnd="true"
     android:text="文件名" />
   <EditText
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/etContent"
     android:layout_below="@+id/etName"
     android:layout_alignParentLeft="true"
     android:layout_alignParentStart="true"
     android:layout_alignParentRight="true"
     android:layout_alignParentEnd="true"
     android:text="文件内容" />
   <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="保存"
     android:id="@+id/btnWrite"
     android:layout_alignTop="@+id/btnRead"
     android:layout_toLeftOf="@+id/btnRead"
     android:layout_toStartOf="@+id/btnRead" />
   <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="读取"
     android:id="@+id/btnRead"
     android:layout_below="@+id/etContent"
     android:layout_alignParentRight="true"
     android:layout_alignParentEnd="true" />
 </RelativeLayout>

 

延伸 · 阅读

精彩推荐