Android数据存储练习2
使用Java IO流存储.dat文件,存储输入数据后可点击查看进入查看页面查看文件存储内容,实现简单记事本工具。
运行效果:
MainActivity.java代码
package com.example.notepad;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editText;
private Button btnNew,btnLook;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
btnNew = findViewById(R.id.btnNew);
btnLook = findViewById(R.id.btnLook);
//设置点击事件
btnNew.setOnClickListener(MainActivity.this);
btnLook.setOnClickListener(MainActivity.this);
}
@Override
public void onClick(View v) {//重写onClick方法
//switch语句控制两个按钮点击函数
switch (v.getId()){
case R.id.btnNew:
saveData();
break;
case R.id.btnLook:
gotoDetail();
break;
}
}
private void gotoDetail(){
//intent跳转到查看页面
Intent intent = new Intent(MainActivity.this,DetailActivity.class);
startActivity(intent);
}
private void saveData(){
PrintWriter pw = null;
try {
FileOutputStream fos = openFileOutput("myfile.dat", Context.MODE_APPEND);
pw = new PrintWriter(fos);
pw.println(editText.getText().toString());
pw.flush();
}catch (FileNotFoundException e){
e.printStackTrace();
}finally {
pw.close();
}
editText.setText("");
Toast.makeText(MainActivity.this,"记录已保存",Toast.LENGTH_SHORT);
}
}
activity_main代码
<?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"
android:padding="8dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="left|top"
android:inputType="textMultiLine"
android:lines="4"
android:maxLines="8"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.025" />
<Button
android:id="@+id/btnNew"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="新增"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/btnLook"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="查看"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnNew" />
</androidx.constraintlayout.widget.ConstraintLayout>
DetailActivity.java
package com.example.notepad;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class DetailActivity extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
tv = findViewById(R.id.tv);
readData();
}
private void readData(){
BufferedReader br = null;
try {
FileInputStream fis = openFileInput("myfile.dat");
br = new BufferedReader(new InputStreamReader(fis));
String ln;
while ((ln = br.readLine()) != null){
tv.append(ln+"\n");
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
br.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
activity_detail.xml代码
<?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=".DetailActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>