Android中想要存储一部分数据在文件中,数据量不会很大,因为文件最终也是存储在手机内存中,数据量太大,则会导致文件的体积增大。
采用文件存储的方式存储数据,实际上与Java对文件读写是一样的操作,字符流读写、字节流读写均可。如果你java关于文件操作学得还不错,这部分可以直接跳过。
第1步:布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="展示信息" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入测试数据" />
<Button
android:id="@+id/writeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存信息" />
<Button
android:id="@+id/readBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取信息" />
</LinearLayout>
第2步:逻辑文件
package com.yibinu.filedemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
/*1、组件*/
EditText editText;
TextView textView;
Button writeBtn, readBtn;
String fineName = "test.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*2、寻找组件*/
editText = findViewById(R.id.editText);
textView = findViewById(R.id.textView);
writeBtn = findViewById(R.id.writeBtn);
readBtn = findViewById(R.id.readBtn);
/*3、设置监听*/
writeBtn.setOnClickListener(this);
readBtn.setOnClickListener(this);
}
/*4、实现接口方法*/
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.writeBtn) {
/*写入数据到文件*/
try {
FileOutputStream fos = openFileOutput(fineName, Context.MODE_APPEND);
fos.write(editText.getText().toString().getBytes());
Toast.makeText(this, "保存的数据是:" + editText.getText().toString(), Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if (id==R.id.readBtn) {
/*从文件中读取数据放入程序*/
try {
FileInputStream fis = openFileInput(fineName);
byte[] buff = new byte[1024];
int hasRead = 0;
StringBuilder sb = new StringBuilder("");
while ((hasRead=fis.read(buff))>0){
sb.append(new String(buff,0,hasRead));
}
String readContent = sb.toString();
textView.setText(readContent);
Toast.makeText(this, "读取到的数据是:"+ readContent, Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
需要注意的是:开发者的站位应该是站在程序这一边,对程序来说,其他文件的内容要进入到程序,那就是输入,也就是对应的读操作;程序中的内容输出到文件中保存,也就是输出,对应的是写操作
核心函数是:openFileInput和openFileOutput
如果操作的是字符居多,使用字符流读写更加方便。
第3步:测试
文件读写效果