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

    }

}
时间: 2024-08-10 21:16:41

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

Android课程---关于数据存储的学习(3)之数据库和事务

DataActivity3.java package com.hanqi.test5; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android

Android Learning:数据存储方案归纳与总结

前言 最近在学习<第一行android代码>和<疯狂android讲义>,我的感触是Android应用的本质其实就是数据的处理,包括数据的接收,存储,处理以及显示,我想针对这几环分别写一篇博客,记得我的学习心得,也希望跟各位新手同学相互努力促进.今天这篇博客,我想介绍一下数据的存储,因为数据的接收,存储,处理以及显示这几环环环相扣,而数据的存储直接关系到数据的处理和显示,所以显得尤为重要. 所以本文针对数据存储的常见方案和其使用进行了归纳.分为程序内存储和程序间数据访问,程序内存储

Android开发之数据存储的四种方式之SharedPreferences

Android项目开发中使用的数据存储方式有:网络存储.sqlite存储.File存储和SharedPreferences存 储,四种存储方式对应的Demo别人是NetworkDemo.SqliteDemo.FileDemo和SharedPreferencesDemo, 根据应用的场景选择其中一种或多种方式,比如在登录界面验证,需要将用户名和密码通过SharedPreferences方式保存,注册信息的时候需要通 过网络将数据存储到后台数据库中.结合一个登录界面的验证,使用SharedPrefe

Android下的数据存储与訪问 --- 以文件的形式

Android下的数据存储与訪问 --- 以文件的形式 1.1 储存文件存放在手机内存中: // *** 储存数据到 /data/data/包名/files/jxn.txt文件里 String data = "test"; // /data/data/包名/files File filesDir = context.getFilesDir(); File file = new File(filesDir, "jxn.txt"); FileOutputStream f

67.Android中的数据存储总结

转载:http://mp.weixin.qq.com/s?__biz=MzIzMjE1Njg4Mw==&mid=2650117688&idx=1&sn=d6c73f9f04d02ede7a741b45e801d74e#rd 本篇文章会帮助大家快速了解Android中各种数据存储机制的应用场景以及基本使用方法,力求在头脑中建立一个“目录”.有了这个目录,具体使用细节在实际应用时再查询文档即可得到. 0. 概述 Android为我们提供了以下存储机制: Shared Preferenc

Android下的数据存储与访问(1) --- 以文件的形式

Android下的数据存储与访问(1) --- 以文件的形式 1.1 储存文件存放在手机内存中: // *** 储存数据到 /data/data/包名/files/jxn.txt文件中 String data = "test"; // /data/data/包名/files File filesDir = context.getFilesDir(); File file = new File(filesDir, "jxn.txt"); FileOutputStrea

Android开发实践 数据存储

所有应用程序必然有数据的输入输出,Android也是一样的,Android应用程序的参数设置.运行状态数据这些都需要保存到外部存储器上,保证关机后数据不丢失,如下是几种Android常见的数据存储方式: SharedPreferences:适合保存少量数据(一些配置信息.积分之类): SQLite:一个真正轻量级数据库,没有后台进程,整个数据库就对应于一个文件,适合大量数据需要存储.访问的情况. 下来详细来看看它们的用法. 1.SharedPreferences SharedPreference

Android入门笔记 - 数据存储 - SharedPreferences

接下来四篇我们来介绍Android中用于数据存储的四种方式: SharedPreferences Sqlite Files 网络 今天我们先来看一个最简单的:SharedPreferences. 这种数据存储方式是最简单,最轻便,也最实用的,但是只能用来储存基本数据类型.我们来看看怎么使用: 1. res/ layout/ activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an

Android中的数据存储

Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedPreferences对象和SQLite数据库,它们的数据仅对创建它们的应用是可访问的. (比如,MODE_WORLD_READABLE 和 MODE_WORLD_WRITEABLE现在(since API 17)已经被标记为deprecated). 换句话说,它们不是共享的,如果需要在不同的应用之间