Android课程---关于数据存储的学习(2)

手机外部存储的学习

activity_data2.xml

<?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_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.dell.shujucunchu.SDkacunchu"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_5"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_6"/>
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="读包的目录"
        android:onClick="baocun3"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="读自定义目录"
        android:onClick="baocun4"/>

</LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="存包目录"
            android:onClick="baocun5"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="存自定义目录"
            android:onClick="baocun6"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="保存到带包名的目录"
            android:onClick="baocun7"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="从带包名目录读取"
            android:onClick="baocun8"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="保存到自定义的目录"
            android:onClick="baocun9"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="从自定义目录读取"
            android:onClick="baocun10"/>

    </LinearLayout>
</LinearLayout>

DataActivity2.java

package com.hanqi.test5;

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

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

public class DataActivity2 extends AppCompatActivity {

    EditText et_5 ;

    EditText et_6 ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data2);
    }
    public void baocun5(View view)
    {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            //1、获取要存储的内容
            et_5 = (EditText)findViewById(R.id.et_5);


            String content = et_5.getText().toString();

//           String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();
//
//            Toast.makeText(SDkacunchu.this, "sdpath="+sdpath, Toast.LENGTH_SHORT).show();

            //参数  代表不同文件类型的子目录,如果没有就穿null

            String sdpath = getExternalFilesDir(null).getAbsolutePath();

            Toast.makeText(DataActivity2.this, "sdpath =" + sdpath, Toast.LENGTH_SHORT).show();

            //构造输出流

            sdpath += "/sd";

            try {
                FileOutputStream fos = new FileOutputStream(sdpath);

                //传统模式  字节数组方式

                fos.write(content.getBytes("utf-8"));

                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        else
        {
            Toast.makeText(DataActivity2.this, "未发现sd卡", Toast.LENGTH_SHORT).show();
        }

    }


    public void baocun3(View view)
    {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            //1、获取要存储的内容
            et_6 = (EditText)findViewById(R.id.et_6);


            //String content = et_6.getText().toString();

//           String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();
//
//            Toast.makeText(SDkacunchu.this, "sdpath="+sdpath, Toast.LENGTH_SHORT).show();

            //参数  代表不同文件类型的子目录,如果没有就穿null

            String sdpath = getExternalFilesDir(null).getAbsolutePath();

            //Toast.makeText(SDkacunchu.this, "sdpath =" + sdpath, Toast.LENGTH_SHORT).show();

            //构造输出流

            sdpath += "/sd";

            try {
                FileInputStream fis = new FileInputStream(sdpath);

                //传统模式  字节数组方式

                byte[]  b = new byte[1024];

                int i =0;

                StringBuilder str = new StringBuilder();

                while ((i=fis.read(b)) > 0) {

                    et_6.setText(str.append(new String(b, 0, i)));


                }

                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        else
        {
            Toast.makeText(DataActivity2.this, "未发现sd卡", Toast.LENGTH_SHORT).show();
        }

    }

    public void baocun6(View view)
    {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            //1、获取要存储的内容
            et_5 = (EditText)findViewById(R.id.et_5);


            String content = et_5.getText().toString();
            //获取外部存储根目录

            String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();
            //在sd卡根目录下再创建子目录
            sdpath += "/hanqi";

            File file = new File(sdpath);
            //如果不存在
            if (!file.exists())
            {
                //创建目录
                file.mkdir();
                //创建文件
                //file.createNewFile();
            }
            Toast.makeText(DataActivity2.this, "sdpath="+sdpath, Toast.LENGTH_SHORT).show();

            //构造输出流

            sdpath += "/test.txt";

            try {
                FileOutputStream fos = new FileOutputStream(sdpath);

                //传统模式  字节数组方式

                fos.write(content.getBytes("utf-8"));

                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        else
        {
            Toast.makeText(DataActivity2.this, "未发现sd卡", Toast.LENGTH_SHORT).show();
        }

    }

    public void baocun4(View view)
    {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            //1、获取要存储的内容
            et_6 = (EditText)findViewById(R.id.et_6);


            //String content = et_6.getText().toString();

            String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();

            Toast.makeText(DataActivity2.this, "sdpath="+sdpath, Toast.LENGTH_SHORT).show();


            //构造输出流

            sdpath += "/hanqi/test.txt";

            try {
                FileInputStream fis = new FileInputStream(sdpath);

                //传统模式  字节数组方式

                byte[]  b = new byte[1024];

                int i =0;

                StringBuilder str = new StringBuilder();

                while ((i=fis.read(b)) > 0) {

                    et_6.setText(str.append(new String(b, 0, i)));


                }

                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        else
        {
            Toast.makeText(DataActivity2.this, "未发现sd卡", Toast.LENGTH_SHORT).show();
        }

    }

}

 

posted @ 2016-04-17 19:36  秦萧不再  阅读(191)  评论(0编辑  收藏  举报