当前位置:   article > 正文

Android 移动开发之数据存储一:临时存储和文件存储(实验6)_android数据本地存储实验

android数据本地存储实验

Android 移动开发之数据存储一:临时存储和文件存储(实验6)

一、实验目的

1、了解Android数据存储的基本方法

2、掌握SharedPreferences、内部文件存储、外部存储技术

二、实验内容

1、尝试使用Shared Preferences在程序关闭时保存用户输入的信息,并在程序重新启动时自动恢复这些信息。

2、以INI文件形式,将数据保存在内部或外部存储器上,以实现相同的功能、

三、实验步骤及代码

1、SharedPreferences存储

1)创建新项目:SharedPreferencesDemo,实现的功能是一个联系人详情展示页,首先做出主界面:

请添加图片描述

<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity1">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="45dp"
        android:text="联系人"
        android:textSize="20dp"
        app:layout_constraintBottom_toTopOf="@+id/NameS"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="66dp"
        android:text="姓名:"
        app:layout_constraintEnd_toStartOf="@+id/NameS"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/NameS" />

    <TextView
        android:id="@+id/Phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="66dp"
        android:text="电话:"
        app:layout_constraintEnd_toStartOf="@+id/PhoneS"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/PhoneS" />

    <TextView
        android:id="@+id/Email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="66dp"
        android:text="邮箱:"
        app:layout_constraintEnd_toStartOf="@+id/EmailS"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/EmailS" />

    <TextView
        android:id="@+id/Address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="66dp"
        android:text="地址:"
        app:layout_constraintEnd_toStartOf="@+id/AddressS"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/AddressS" />

    <TextView
        android:id="@+id/NameS"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="63dp"
        android:layout_marginBottom="30dp"
        android:text=""
        app:layout_constraintBottom_toTopOf="@+id/PhoneS"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/Name"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/PhoneS"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="63dp"
        android:layout_marginBottom="25dp"
        android:text=""
        app:layout_constraintBottom_toTopOf="@+id/EmailS"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/Phone"
        app:layout_constraintTop_toBottomOf="@+id/NameS" />

    <TextView
        android:id="@+id/EmailS"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="63dp"
        android:layout_marginBottom="27dp"
        android:text=""
        app:layout_constraintBottom_toTopOf="@+id/AddressS"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/Email"
        app:layout_constraintTop_toBottomOf="@+id/PhoneS" />

    <TextView
        android:id="@+id/AddressS"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="63dp"
        android:layout_marginBottom="61dp"
        android:text=""
        app:layout_constraintBottom_toTopOf="@+id/Edit"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/Address"
        app:layout_constraintTop_toBottomOf="@+id/EmailS" />

    <Button
        android:id="@+id/Edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="304dp"
        android:text="Edit"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/AddressS" />

    <Button
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="2dp"
        android:layout_marginTop="15dp"
        android:text="show"
        app:layout_constraintStart_toStartOf="@+id/Edit"
        app:layout_constraintTop_toBottomOf="@+id/Edit" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133

2)在MainActivity1中实现打开SharedPreferences并显示数据的功能

package com.example.sharedpreferencesdemo;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;



public class MainActivity1 extends AppCompatActivity implements View.OnClickListener {
Button Edit;
Button Show;
TextView Name;
TextView Phone;
TextView Email;
TextView Address;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main1);
        Name=findViewById(R.id.NameS);
        Phone=findViewById(R.id.PhoneS);
        Email=findViewById(R.id.EmailS);
        Address=findViewById(R.id.AddressS);
        Show=findViewById(R.id.show);


        Edit=findViewById(R.id.Edit);
        Edit.setOnClickListener((View.OnClickListener) this);
        Show.setOnClickListener((View.OnClickListener) this);
    }

    public void onClick(View view){
        int id=view.getId();
        switch (id){
            case R.id.Edit:
                Intent intent=new Intent();
                intent.setClass(MainActivity1.this,MainActivity2.class);
                startActivities(new Intent[]{intent});
                break;
            case R.id.show:
                Show();
                break;
        }
    }

    public void Show(){
        SharedPreferences sharedPreferences = getSharedPreferences("userDATA", MODE_PRIVATE);
        String name=sharedPreferences.getString("NAME", "Default Name");
        String phone=sharedPreferences.getString("PHONE", "Default Phone");
        String email=sharedPreferences.getString("EMAIL", "Default Email");
        String address=sharedPreferences.getString("ADDRESS", "Default Address");
        Name.setText(name);
        Phone.setText(phone);
        Email.setText(email);
        Address.setText(address);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

3)新建一个MainActivity2,在这里实现编辑数据并保存的功能,界面布局activity_main2如下
在这里插入图片描述

<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="53dp"
        android:text="通讯录"
        android:textSize="20dp"
        app:layout_constraintBottom_toTopOf="@+id/nameE"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名"
        app:layout_constraintBottom_toTopOf="@+id/nameE"
        app:layout_constraintEnd_toEndOf="@+id/phone"
        app:layout_constraintStart_toStartOf="@+id/phone" />

    <TextView
        android:id="@+id/phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="电话"
        app:layout_constraintBottom_toTopOf="@+id/phoneE"
        app:layout_constraintEnd_toEndOf="@+id/email"
        app:layout_constraintStart_toStartOf="@+id/email" />

    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="邮箱"
        app:layout_constraintBottom_toTopOf="@+id/emailE"
        app:layout_constraintEnd_toEndOf="@+id/address"
        app:layout_constraintStart_toStartOf="@+id/address" />

    <TextView
        android:id="@+id/address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="地址"
        app:layout_constraintBottom_toTopOf="@+id/addressE"
        app:layout_constraintStart_toStartOf="@+id/addressE" />

    <EditText
        android:id="@+id/nameE"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="48dp"
        app:layout_constraintBottom_toTopOf="@+id/phoneE"
        app:layout_constraintEnd_toEndOf="@+id/phoneE"
        app:layout_constraintStart_toStartOf="@+id/name"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/phoneE"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="46dp"
        app:layout_constraintBottom_toTopOf="@+id/emailE"
        app:layout_constraintEnd_toEndOf="@+id/emailE"
        app:layout_constraintStart_toStartOf="@+id/phone"
        app:layout_constraintTop_toBottomOf="@+id/nameE" />

    <EditText
        android:id="@+id/emailE"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="36dp"
        app:layout_constraintBottom_toTopOf="@+id/addressE"
        app:layout_constraintEnd_toEndOf="@+id/addressE"
        app:layout_constraintStart_toStartOf="@+id/email"
        app:layout_constraintTop_toBottomOf="@+id/phoneE" />

    <EditText
        android:id="@+id/addressE"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="45dp"
        android:layout_marginEnd="45dp"
        android:layout_marginBottom="306dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/emailE" />

    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="44dp"
        android:text="Save"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/addressE" />


</androidx.constraintlayout.widget.ConstraintLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111

4)在MainActivity2中实现其输入并保存的功能

package com.example.sharedpreferencesdemo;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

    EditText Name;
    EditText Phone;
    EditText Email;
    EditText Address;
    Button Save;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main2);
        Name=findViewById(R.id.nameE);
        Phone=findViewById(R.id.phoneE);
        Email=findViewById(R.id.emailE);
        Address=findViewById(R.id.addressE);
        Save=findViewById(R.id.save);
        Save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Save();
            }
        });
    }

    public static int MODE =MODE_PRIVATE;

    public void Save(){
        SharedPreferences.Editor editor=getSharedPreferences("userDATA",MODE).edit();
        editor.putString("NAME", String.valueOf(Name.getText()));
        editor.putString("PHONE", String.valueOf(Phone.getText()));
        editor.putString("EMAIL", String.valueOf(Email.getText()));
        editor.putString("ADDRESS", String.valueOf(Address.getText()));
        editor.apply();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

5)存储的数据文件打开方式:

View -> Tool Windows -> Device Explore

保存在文件夹/data/data//shared_prefs目录下
在这里插入图片描述
在这里插入图片描述

文件打开后是这样的:
在这里插入图片描述

6)整个项目,主要结构如下

在这里插入图片描述

7)运行结果

2、内部存储

1)新建项目FileStoreDemo,要实现的是一个记事本功能,可以输入并保存文字,布局如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:gravity="center_horizontal"
        android:text="记事本"
        android:textSize="18sp"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/black"/>

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:layout_weight="1"
        android:gravity="start"
        android:hint="请输入文本"
        android:inputType="textMultiLine"
        />


    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:onClick="saveFile"
        android:text="Save"
        />



</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

2)MainActivity功能实现

package com.example.filestoredemo;

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 androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
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 MainActivity extends AppCompatActivity {

    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        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());
    }

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();
            }
        }
    }
}

    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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115

3)项目结构很简单
在这里插入图片描述

4)运行结果

在这里插入图片描述
在这里插入图片描述

再次重新启动软件后便会自动加载文件并显示

5)查看文件

在这里插入图片描述

3、外部存储

1)新建一个FileStoreDemo1,布局:

<?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:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="vertical"
    tools:context=".FileStoreDemo1"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:gravity="center_horizontal"
        android:text="记事本"
        android:textSize="18sp"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/black"/>

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:gravity="start"
        android:hint="请输入文本"
        android:inputType="textMultiLine"
        />

    <TextView
        android:id="@+id/Show"
        android:layout_width="match_parent"
        android:layout_height="400dp"

        />


    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:onClick="saveFile"
        android:text="Save"
        />




</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

2)FileStoreDemo1.java

package com.example.filestoredemo1;

import android.Manifest;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

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

public class FileStoreDemo1 extends AppCompatActivity {

    private File dir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
    private File dataFile=new File(dir,"data.txt");
    Button save;

    EditText inputText;
    TextView Show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_file_store_demo1);
        save=findViewById(R.id.save);

        inputText=findViewById(R.id.edit_text);
        Show=findViewById(R.id.Show);

        //动态询问获取权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            System.out.println("ok");
        }else {
            ActivityCompat.requestPermissions(this,new String[] {
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            },1);
        }

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                write();
                read();
            }
        });


    }



    private void write(){
        try{
            if(!dataFile.exists()){
                dataFile.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(dataFile);
            String input = inputText.getText().toString();
            fos.write(input.getBytes("utf-8"));
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            Log.e("FileStoreDemo1", "FileNotFoundException", e);
        } catch (IOException e) {
            Log.e("FileStoreDemo1", "IOException", e);
        }
    }

    private void read(){
        try{
            FileInputStream fis = new FileInputStream(dataFile);
            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);
            fis.close();
            String str = new String(bytes, "utf-8");
            if(Show != null) {
                Show.setText(str);
            }
            Log.i("FileStoreDemo1", str);
        } catch (FileNotFoundException e) {
            Log.e("FileStoreDemo1", "FileNotFoundException", e);
        } catch (IOException e) {
            Log.e("FileStoreDemo1", "IOException", e);
        }
    }



}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104

3)声明权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  • 1
  • 2

4)运行结果

在这里插入图片描述

5)查看文件
在这里插入图片描述
在这里插入图片描述

需要技术支持的话439245287

扣扣

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/843085
推荐阅读
相关标签
  

闽ICP备14008679号