Android开发——数据存储(二)File内部存储

版权声明:如转载请表明出处 https://blog.csdn.net/weixin_42247720/article/details/89519606

Android开发——数据存储(二)File内部存储

内部存储特点

在这里插入图片描述

代码示例

FileStorageActivity的java文件

package com.example.administrator.exercise.datastorage;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.exercise.R;
import com.example.administrator.exercise.util.ToastUtil;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileStorageActivity extends AppCompatActivity {

    private EditText editText;
    private Button button_save,button_display;
    private TextView textView;
    private final static String filename= "text.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_storage);
        editText = (EditText) findViewById(R.id.fs_et_input);
        textView = (TextView) findViewById(R.id.fs_tv_display);
        //连接保存按钮
        button_save = (Button) findViewById(R.id.fs_btn_save);
        //连接显示按钮
        button_display = (Button) findViewById(R.id.fs_btn_display);

        button_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                writeData(editText.getText().toString());
                ToastUtil.showMsg(FileStorageActivity.this,"保存成功");
            }
        });
        button_display.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText(readData());
                ToastUtil.showMsg(FileStorageActivity.this,"读取成功");
            }
        });
    }

    //存储数据
    private void writeData(String content){
        FileOutputStream fileOutputStream = null;
        try {
            //创建存储目标
            fileOutputStream = openFileOutput(filename,MODE_PRIVATE);
            //字节方式存储方法
            fileOutputStream.write(content.getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            //关闭流
            if(fileOutputStream!=null)
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //读取数据
    private String readData(){
        FileInputStream fileInputStream = null;
        try {
            //获取读取文件
            fileInputStream = openFileInput(filename);
            //设置一次读取字节数
            byte[] buff = new byte[1024];
            //获取stringBuilder
            StringBuilder stringBuilder = new StringBuilder("");
            int len = 0;
            //循环读取
            while((len = fileInputStream.read(buff)) > 0 ){
                stringBuilder.append(new String(buff,0,len));
            }
            //返回读取数据
            return stringBuilder.toString();
        }  catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            //关闭流
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_shared_preferences"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="请输入数据"
        android:id="@+id/fs_et_input"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="保存"
        android:textSize="16sp"
        android:id="@+id/fs_btn_save"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="显示"
        android:textSize="16sp"
        android:id="@+id/fs_btn_display"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/fs_tv_display"/>
</LinearLayout>

运行结果图

在这里插入图片描述

存储位置

data/data/ApplicationID(包名)/files/文件名

在这里插入图片描述

总结

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42247720/article/details/89519606
今日推荐