手机外部存储的学习

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. html用户注册界面

    html用户注册界面   先上一张简约的界面的效果图 这里是style里面的内容 <style> input[type]{ border: 1px solid darkorange; ba ...

  2. TouchSlide1.1,手机上的幻灯片

    TouchSlide 是纯javascript打造的触屏滑动特效插件 http://pan.baidu.com/s/1bpoWNin 官网:http://www.superslide2.com/Tou ...

  3. sublime3侧边栏颜色修改,推荐主题

    sublime侧边栏的颜色默认是灰白色的,下面方法可以手动定制颜色为深色: 需要修改的文件为: C:\program\Sublime\Packages\Theme - Default.sublime- ...

  4. SQL,Linq,Lambda之间的转换练习

    1.查询Student表中的所有记录的Sname.Ssex和Class列. SQL:select sname,ssex,class from Students linq:from s in Stude ...

  5. poj 3264(线段树)

    http://poj.org/problem?id=3264 初学线段可以做的水题,也是线段树的基础运用.也是我的第一个线段树的题. 题意:在区间范围内的最大值减去最小值 思路:线段树记录下每个区间内 ...

  6. eclipse 粘贴字符串自动添加转义符

    eclipse -> Window -> Preferences -> Java -> Editor -> Typing -> 勾选{Escape text whe ...

  7. 懵逼的闭包--for循环(转)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. NIO初识

    Java编程中的NIO,俗称new I/O,是在JDK1.4版本之后开始引入的,在JDK1.4之前,Java服务端大多使用同步阻塞式来处理网络请求,在低流量.低并发情况还能抗住,在如今互联网时代,信息 ...

  9. linux下使用automake工具自动生成makefile文件

    linux环境下,当项目工程很大的时候,编译的过程很复杂,所以需要使用make工具,自动进行编译安装,但是手写makefile文件比较复杂,所幸在GNU的计划中,设计出了一种叫做Autoconf/Au ...

  10. Pyqt 获取动态生成的QLineEdit值

    动态生成控件参考上一篇: http://www.cnblogs.com/dcb3688/p/4588814.html 那如何获取动态生成控件的值呢? 比如,动态的生成10个输入框QLineEdit,输 ...