安卓数据存储方式之IO存储

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="com.example.cookie.android0623data.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_main_name"
        android:hint="请输入用户名"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="5"
        android:id="@+id/et_main_text"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存"
        android:onClick="save"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开"
        android:onClick="read"/>
    </LinearLayout>

</LinearLayout>



JAVA代码:

package com.example.cookie.android0623data;

import android.content.Context;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

    private EditText et_main_text;
    private EditText et_main_name;
    private String sdCard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_main_text = (EditText) findViewById(R.id.et_main_text);
        et_main_name = (EditText) findViewById(R.id.et_main_name);
        //获取手机内存卡的路径
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            sdCard = Environment.getExternalStorageDirectory().getAbsolutePath();
        }

    }

    public void save(View view){
        String content=et_main_text.getText().toString();
        String fileName=et_main_name.getText().toString();
        //存东西,输出流OutputStream
        try {
            //存储在手机内存里面
            //FileOutputStream fos=openFileOutput(fileName, Context.MODE_PRIVATE);
            //存储在内存卡里面
            FileOutputStream fos=new FileOutputStream(sdCard+"/"+fileName);
            fos.write(content.getBytes());
            fos.close();
            Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void read(View view){
        String fileName=et_main_name.getText().toString();
        //读,输入流Input
        try {
            //存储在手机内存里
          // FileInputStream fis= openFileInput(fileName);
            //存储在手机内存卡里面
            FileInputStream fis=new FileInputStream(sdCard+"/"+fileName);
            byte buf[]=new byte[1024];
            int len=0;
            StringBuffer s=new StringBuffer();
            while((len=fis.read(buf))!=-1){
                s.append(new String(buf,0,len));
            }
            et_main_text.setText(s);
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

存储在手机内存卡里面要在manifests下的AndroidManifest.xml里面给权限:

    <!-- 内存卡读和写的权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


存储在手机内存里面的查看文件的路径是:

在夜神模拟器里面的查看是:文件管理------》data--------->data---------->项目的包名------------》files-------------->文件名


存储在手机内存卡里面查看文件的路径是:

文件管理------>mnt---------->sdcard---------->文件名

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值