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 import android.support.v7.app.AppCompatActivity;
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 AppCompatActivity {
12
13 private EditText etName;
14 private EditText etContent;
15 private Button btnWrite;
16 private Button btnRead;
17 private String fileName = "";
18 private String fileContent = "";
19
20 @Override
21 protected void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.activity_main);
24
25 etName = (EditText)findViewById(R.id.etName);
26 etContent = (EditText)findViewById(R.id.etContent);
27 btnWrite = (Button)findViewById(R.id.btnWrite);
28 btnRead = (Button)findViewById(R.id.btnRead);
29
30 btnWrite.setOnClickListener(new View.OnClickListener() {
31 @Override
32 public void onClick(View view) {
33 fileName = etName.getText().toString();
34 fileContent = etContent.getText().toString();
35 try {
36 FileOutputStream foStream = openFileOutput(fileName, MODE_PRIVATE);
37 byte[] buffer = fileContent.getBytes();
38 foStream.write(buffer);
39 Toast.makeText(MainActivity.this, "写入成功",Toast.LENGTH_SHORT).show();
40 foStream.flush();
41 foStream.close();
42 }catch(Exception e){
43 e.printStackTrace();
44 }
45 }
46 });
47 btnRead.setOnClickListener(new View.OnClickListener() {
48 @Override
49 public void onClick(View view) {
50 fileName = etName.getText().toString();
51 try{
52 FileInputStream fiStream = openFileInput(fileName);
53 int len = fiStream.available();
54 byte[] buffer = new byte[len];
55 fiStream.read(buffer);
56 etContent.setText(new String(buffer));
57 Toast.makeText(MainActivity.this, "读取成功",Toast.LENGTH_SHORT).show();
58 fiStream.close();
59 }catch(Exception e){
60 e.printStackTrace();
61 }
62 }
63 });
64
65 }
66 }
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
3 android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
4 android:paddingRight="@dimen/activity_horizontal_margin"
5 android:paddingTop="@dimen/activity_vertical_margin"
6 android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
7
8
9 <EditText
10 android:layout_width="wrap_content"
11 android:layout_height="wrap_content"
12 android:id="@+id/etName"
13 android:layout_alignParentTop="true"
14 android:layout_alignParentLeft="true"
15 android:layout_alignParentStart="true"
16 android:layout_alignParentRight="true"
17 android:layout_alignParentEnd="true"
18 android:text="文件名" />
19
20 <EditText
21 android:layout_width="wrap_content"
22 android:layout_height="wrap_content"
23 android:id="@+id/etContent"
24 android:layout_below="@+id/etName"
25 android:layout_alignParentLeft="true"
26 android:layout_alignParentStart="true"
27 android:layout_alignParentRight="true"
28 android:layout_alignParentEnd="true"
29 android:text="文件内容" />
30
31 <Button
32 android:layout_width="wrap_content"
33 android:layout_height="wrap_content"
34 android:text="保存"
35 android:id="@+id/btnWrite"
36 android:layout_alignTop="@+id/btnRead"
37 android:layout_toLeftOf="@+id/btnRead"
38 android:layout_toStartOf="@+id/btnRead" />
39
40 <Button
41 android:layout_width="wrap_content"
42 android:layout_height="wrap_content"
43 android:text="读取"
44 android:id="@+id/btnRead"
45 android:layout_below="@+id/etContent"
46 android:layout_alignParentRight="true"
47 android:layout_alignParentEnd="true" />
48 </RelativeLayout>
activity_main.xml