场景
在Android应用中需要将某些文本内容保存到存储器中的txt文件中。
在显示时还需要将txt文件的内容读取加载出来。形成一个简单记事本的效果
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
首先新建一个Activity并设计页面布局如上,xml布局代码为
<?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=".StoreTextActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:gravity="center_horizontal" android:textSize="18sp" android:textColor="@android:color/black" android:text="记事本" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#FFC8C8C8"/> <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="start" android:hint="请输入文本" android:inputType="textMultiLine" /> <Button android:id="@+id/btn_save" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="saveFile" android:text="保存" /> </LinearLayout>
然后在Activity,在保存按钮的点击事件执行的保存到文件的方法中,将editText的文本内容保存到文件
/*** * 保存到文件 * @param str */ public void saveFile(String str) { FileOutputStream fos = null; BufferedWriter writer = null; try { fos = openFileOutput("badao.txt", Context.MODE_PRIVATE); writer = new BufferedWriter(new OutputStreamWriter(fos)); try { writer.write(str); Toast.makeText(this,"保存成功",Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally { if(writer!=null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } if(fos!=null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
保存成功后,文件会在内部存储下data/data/包名/files下找到
然后在onCreate方法中,首先执行从txt加载内容的方法
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_store_text); editText = findViewById(R.id.edit_text); String str = load(); if(!(TextUtils.isEmpty(str))) { editText.setText(str); Toast.makeText(this,"加载成功",Toast.LENGTH_LONG).show(); } }
在加载内容的方法load中
/*** * 从文件中加载 * @return */ public String load() { FileInputStream fis = null; BufferedReader reader = null; StringBuilder content = new StringBuilder(); try { fis = openFileInput("badao.txt"); reader = new BufferedReader(new InputStreamReader(fis)); String str; while ((str=reader.readLine())!=null) { content.append(str); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(reader!=null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if(fis!=null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return content.toString(); }
activity的完整代码
package com.badao.androidstudy; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class StoreTextActivity extends AppCompatActivity { private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_store_text); editText = findViewById(R.id.edit_text); String str = load(); if(!(TextUtils.isEmpty(str))) { editText.setText(str); Toast.makeText(this,"加载成功",Toast.LENGTH_LONG).show(); } } public void saveFile(View view) { saveFile(editText.getText().toString()); } /*** * 从文件中加载 * @return */ public String load() { FileInputStream fis = null; BufferedReader reader = null; StringBuilder content = new StringBuilder(); try { fis = openFileInput("badao.txt"); reader = new BufferedReader(new InputStreamReader(fis)); String str; while ((str=reader.readLine())!=null) { content.append(str); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(reader!=null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if(fis!=null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return content.toString(); } /*** * 保存到文件 * @param str */ public void saveFile(String str) { FileOutputStream fos = null; BufferedWriter writer = null; try { fos = openFileOutput("badao.txt", Context.MODE_PRIVATE); writer = new BufferedWriter(new OutputStreamWriter(fos)); try { writer.write(str); Toast.makeText(this,"保存成功",Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally { if(writer!=null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } if(fos!=null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
运行之后,输入一些文本内容,点击保存
然后退出app重新启动后