手机外部存储的学习

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模板引擎正则替换函数 preg_replace 与 preg_replace_callback 使用总结

    在编写PHP模板引擎工具类时,以前常用的一个正则替换函数为 preg_replace(),加上正则修饰符 /e,就能够执行强大的回调函数,实现模板引擎编译(其实就是字符串替换). 详情介绍参考博文:P ...

  2. JAVA设计模式--单例模式

    单例设计模式 Singleton是一种创建型模式,指某个类采用Singleton模式,则在这个类被创建后,只可能产生一个实例供外部访问,并且提供一个全局的访问点. 核心知识点如下: (1) 将采用单例 ...

  3. 利用iis虚拟目录实现文件服务器功能(分布式存储)

    要求说明: 通过网站上传文件保存到统一的文件服务器上. 服务器说明: 1.文件服务器以下称为FilesServer,IP地址为:192.168.1.213 2.Web服务器为以下称为WebServer ...

  4. Postman 发送http请求工具

    http://donglegend.com/2016/10/28/Postman/ Postman 发现一款发送Web API & HTTP 请求的工具,没错,就是Postman.推荐给大家, ...

  5. T-SQL 语句的理解

    1.T-SQL中各子句在逻辑上按照以下顺序进行处理 . . . .. .ORDER BY 查询实例: SELECT EMPID, YEAR(ORDERDATE) AS ORDERYEAR, COUNT ...

  6. core dump 是什么意思?

    core dump,翻译过来讲,就是核心转储.大致上就是指,如果由于应用错误,如浮点异常.指令异常等,操作系统将会转入内核的异常处理,向对应的进程发送特定的信号(SIGNAL),如果进程中没有对这些信 ...

  7. 定时刷新之setTimeout(只一次)和setInterval(间隔相同时间)的使用

    setTimeout和setInterval的使用 这两个方法都可以用来实现在一个固定时间段之后去执行JavaScript.不过两者各有各的应用场景. 方 法 实际上,setTimeout和setIn ...

  8. .NET LINQ 筛选数据

    筛选数据      筛选指将结果集限制为只包含那些满足指定条件的元素的操作. 它又称为选择. 方法 方法名 说明 C# 查询表达式语法 Visual Basic 查询表达式语法 更多信息 OfType ...

  9. js计算地球两个经纬度之间的距离

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. node在centos下的安装

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