手机外部存储的学习

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

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

  1. Android课程---关于数据存储的学习

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

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

    DataActivity3.java package com.hanqi.test5; import android.content.ContentValues; import android.dat ...

  3. Android课程---关于数据存储的学习之总结

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

    前言 最近在学习<第一行android代码>和<疯狂android讲义>,我的感触是Android应用的本质其实就是数据的处理,包括数据的接收,存储,处理以及显示,我想针对这几 ...

  5. Android中的数据存储(二):文件存储 2017-05-25 08:16 35人阅读 评论(0) 收藏

    文件存储 这是本人(菜鸟)学习android数据存储时接触的有关文件存储的知识以及本人自己写的简单地demo,为初学者学习和使用文件存储提供一些帮助.. 如果有需要查看SharedPreference ...

  6. 67.Android中的数据存储总结

    转载:http://mp.weixin.qq.com/s?__biz=MzIzMjE1Njg4Mw==&mid=2650117688&idx=1&sn=d6c73f9f04d0 ...

  7. Android中的数据存储

    Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedP ...

  8. Android五种数据存储方式

    android 五种数据存储 :SharePreferences.SQLite.Contert Provider.File.网络存储 Android系统提供了四种存储数据方式.分别为:SharePre ...

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

    Android下的数据存储与訪问 --- 以文件的形式 1.1 储存文件存放在手机内存中: // *** 储存数据到 /data/data/包名/files/jxn.txt文件里 String dat ...

随机推荐

  1. PHP变量入门教程(2)超全局变量,总共9个

    PHP 超全局变量 $GLOBALS 包含一个引用指向每个当前脚本的全局范围内有效的变量.该数组的键标为全局变量的 名称.从 PHP 3 开始存在 $GLOBALS 数组. $_SERVER 变量由 ...

  2. Windows下安装MongoDB

    项目当中用到MongoDB最为NoSQL数据库,运行的平台为 Windows Server 2008,下面是MongoDB的安装过程笔记: 1.下载软件 官方下载地址:http://www.mongo ...

  3. opencv常见代码

    http://blog.csdn.net/lyc_daniel/article/details/16883707

  4. ecplise 常用快捷键

    /* * alt+/ * * A:main * main+alt+/ * B:输出语句 * syso+alt+/ * C:提示作用 * * */ /* * 常用快捷键 * 1.格式化:ctrl+shi ...

  5. .NET LINQ 联接运算

    联接运算      将两个数据源“联接”就是将一个数据源中的对象与另一个数据源中共享某个通用特性的对象关联起来.      当查询所面向的数据源相互之间具有无法直接领会的关系时,联接就成为一项重要的运 ...

  6. node在centos下的安装

    CentOS 下安装 Node.js 1.下载源码,你需要在https://nodejs.org/en/download/下载最新的Nodejs版本,本文以v4.6.2为例: cd /usr/loca ...

  7. 深入研究java.lang.Runtime类

    一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象, ...

  8. hadoop2的思想架构

    mapreduce 2 思想架构 mr2解决了mr1的jobTracker的单点颈瓶问题,这个问题会影响hadoop的扩展性,集群的可靠性,mr1中jobTracker负责集群作业的分发,管理,调度, ...

  9. Android中Input型输入设备驱动原理分析(一)

    转自:http://blog.csdn.net/eilianlau/article/details/6969361 话说Android中Event输入设备驱动原理分析还不如说Linux输入子系统呢,反 ...

  10. SpringMVC学习(三)整合SpringMVC和MyBatis

    工程结构 导入jar包 配置文件 applicationContext-dao.xml---配置数据源.SqlSessionFactory.mapper扫描器 applicationContext-s ...