文件存储是 Android 中最基本的一种数据存储方式,它不对存储的内容进行任何的格式化处理,所有数据都是原封不动地保存到文件当中的,因而它比较适合用于存储一些简单的文本数据或二进制数据。如果你想使用文件存储的方式来保存一些较为复杂的文本数据,就需要定义一套自己的格式规范,这样方便于之后将数据从文件中重新解析出来。
案例:
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#34718E"
android:text="Save"
app:backgroundTintMode="multiply"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.247"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Message"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/saveButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/readButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#673AB7"
android:text="Read"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.758"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
其对应Activity文件如下:
public class MainActivity extends AppCompatActivity {
final String fileName = "aa";
Button saveButton;
EditText editText;
TextView textView;
Button readButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
saveButton = findViewById(R.id.saveButton);
editText = findViewById(R.id.editText);
textView = findViewById(R.id.textView);
readButton = findViewById(R.id.readButton);
saveButton.setOnClickListener(new SaveButtonActionListener());
readButton.setOnClickListener(new ReadButtonActionListener());
}
class SaveButtonActionListener implements View.OnClickListener {
@Override
public void onClick(View v) {
textView.setText(editText.getText().toString());
save();
}
}
class ReadButtonActionListener implements View.OnClickListener {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
textView.setText(read());
}
}
@RequiresApi(api = Build.VERSION_CODES.N)
private String read() {
FileInputStream inputStream = null;
BufferedReader bufferedReader = null;
StringBuffer buffer = new StringBuffer();
try {
inputStream = openFileInput("aa");
// 字节流与字符流的转换
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
bufferedReader.lines().forEach(s -> {
buffer.append(s).append("\n");
});
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return buffer.toString();
}
public void save() {
FileOutputStream outputStream = null;
BufferedWriter bufferedWriter = null;
try {
// Context.MODE_APPEND 追加
// Context.MODE_PRIVATE 覆盖
outputStream = openFileOutput("aa", Context.MODE_PRIVATE);
// 字节流与字符流的转换
bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write(editText.getText().toString() + "\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
通过以上内容可以实现将 TextView
中的内容保存到文件中,并读取内容。