Android数据存储系列————文本存储

目录

前言

内容要点

操作流程

实践代码

前言

  文本存储一般情况下,用于是存储一段内容比较多的文本信息

内容要点

  熟知FileInputStream、FileOutputStream

  ps:保存文本使用输出流,读取文本使用输入流

操作流程

  保存文本信息

  • 创建输出流FileOutputStream对象
  • 获取输入框输入内容
  • 将数据写入文本中
  • 关闭输出流

读取文本信息

  • 创建输入流FileInputStream对象
  • 实例字节输出流
  • 创建缓存空间
  • 将缓存写入输入流
  • 关闭输出流
  • 关闭输入流
  • 读取文本内容

实践代码

 布局代码:main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/edit_textinput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/button_savetext"
        android:text="保存文本"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/text_textoutput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/button_readtext"
        android:text="读取文本"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

 Java代码:MainActivity.java

package com.hacker.filestream;

import androidx.appcompat.app.AppCompatActivity;

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.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
//    定义文件夹名
    private String fileName="save.txt";
//    文件输出流
    private FileOutputStream fileOutputStream;
//    字节输出流
    ByteArrayOutputStream stream;
//    文件输入流
    private FileInputStream fileInputStream;
    //输入框
    private EditText inputFileText;
    //保存按钮,读取按钮
    private Button saveButton,readButton;
    //输入内容
    private TextView outFileText;
    //输入框的内容
    private String fileText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        inputFileText=findViewById(R.id.edit_textinput);
        saveButton=findViewById(R.id.button_savetext);
        readButton=findViewById(R.id.button_readtext);
        outFileText=findViewById(R.id.text_textoutput);
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WriteFile();
            }
        });
        readButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ReadFile();
            }
        });

    }

    //写入文件,使用输出流
    private void WriteFile(){
        try {
//            私有模式创建文件
            fileOutputStream=openFileOutput(fileName,MODE_PRIVATE);
            //获取输入框输入的内容
            fileText=inputFileText.getText().toString();
            //写入数据
            fileOutputStream.write(fileText.getBytes());
            fileOutputStream.flush();
            //关闭文件输出流
            fileOutputStream.close();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //在finally中关闭流!因为如果找不到数据就会异常,我们也需要对其进行关闭
            try {
                if (fileOutputStream!=null){
                    //这里判断的目的是因为如果没有找到数据的时候,两种流就不会实例化,既然没有实例化则如果调用就会提示null
                    fileOutputStream.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    //读取文件,使用输入流
    private void ReadFile(){
        try {
            //获取指定文件的输入流
            fileInputStream=openFileInput(fileName);
            //实例化字节输出流
            stream=new ByteArrayOutputStream();
            //输入输出缓存
            byte[] buffer=new byte[1024];
            int length=-1;
            //读取文件输入流的内容到缓存中
            while((length=fileInputStream.read(buffer))!=-1){
//                将缓存写入输入流
                stream.write(buffer,0,length);
            }
            //关闭输出流
            stream.close();
            //关闭文件输入流
            fileInputStream.close();
            //读取文本内容
            outFileText.setText(stream.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            //无论是否异常都要关闭输入输出流
        }finally {
            try {
                //输出流不为空,则关闭输出流
                if (stream!=null){
                    stream.close();
                    //输入流不为空,则关闭输入流
                }else if(fileInputStream!=null){
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

posted on 2020-11-05 20:44  迷途行者  阅读(437)  评论(0编辑  收藏  举报

导航