android文件存储

文件存储

文本存储用于存储一些简单的文本数据或者二进制数据,android文件存储通过IO流存储和读数据

  1. 存储

Context类中提供了一个openFileOutput方法。这个方法接收两个参数,第一个参数是文件名,第二个参数是存储模式,有两种(MODE_PRIVATE和MODE_APPEND)MOED_PRIVATE表示文件写入的所有内容都覆盖原有内容,MODE_APPEND表示在原有文件上追加内容。

  1. 读取

Context类中提供了一个openFileInput方法。这个方法接收一个参数,是文件名。

  1. 例子

    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=".MainActivity">
    
        <EditText
            android:id="@+id/save_data_ed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="save"
            android:onClick="save"
           />
    
        <TextView
            android:id="@+id/read_data_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="read"
            android:onClick="read"/>
    </LinearLayout>
    
  2. 定义一个file文件工具类

    注意:不要忘记关闭输入输出流文件读取的时候通过bufferedReader快熟读取,并将其一行一行读出,然后存放于stringBuilder中

    package com.example.textsave;
    
    import android.content.Context;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.nio.charset.StandardCharsets;
    
    public class FileHelper {
    
        Context context;
    
        public FileHelper() {
        }
    
        public FileHelper(Context context) {
            super();
            this.context = context;
        }
    
        public void saveData(String fileName,String writeContent) throws IOException {
            FileOutputStream output = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            output.write(writeContent.getBytes(StandardCharsets.UTF_8));
            output.close();    // 不要忘记关闭输出流
        }
    
        public String readData(String fileName) throws IOException {
            FileInputStream inputStream = context.openFileInput(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            StringBuilder stringBuilder = new StringBuilder();   
            while ((line=reader.readLine())!=null){          // 将文本数据一行一行读出来,并添加到stringbuilder中   
                stringBuilder.append(line);
            }
            inputStream.close();
            return stringBuilder.toString();
        }
    }
    
  3. mainActivity中的操作文件名为hello,文件的内容输入在editView中

package com.example.textsave;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private TextView textView;
    String fileName = "hello";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = findViewById(R.id.save_data_ed);
        textView = findViewById(R.id.read_data_tv);
    }

    public void save(View view) throws IOException {
        FileHelper fileHelper = new FileHelper(getApplicationContext());
        String saveData = editText.getText().toString();
        fileHelper.saveData(fileName,saveData);
    }

    public void read(View view) throws IOException {
        FileHelper fileHelper = new FileHelper(getApplicationContext());
        String readContent = fileHelper.readData(fileName);
        textView.setText(readContent);
    }
}

版权声明:本文为Q1ANfeng原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。