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();
} } }
Android课程---关于数据存储的学习(2)的更多相关文章
- Android课程---关于数据存储的学习
activity_data1.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
- Android课程---关于数据存储的学习(3)之数据库和事务
DataActivity3.java package com.hanqi.test5; import android.content.ContentValues; import android.dat ...
- Android课程---关于数据存储的学习之总结
- Android Learning:数据存储方案归纳与总结
前言 最近在学习<第一行android代码>和<疯狂android讲义>,我的感触是Android应用的本质其实就是数据的处理,包括数据的接收,存储,处理以及显示,我想针对这几 ...
- Android中的数据存储(二):文件存储 2017-05-25 08:16 35人阅读 评论(0) 收藏
文件存储 这是本人(菜鸟)学习android数据存储时接触的有关文件存储的知识以及本人自己写的简单地demo,为初学者学习和使用文件存储提供一些帮助.. 如果有需要查看SharedPreference ...
- 67.Android中的数据存储总结
转载:http://mp.weixin.qq.com/s?__biz=MzIzMjE1Njg4Mw==&mid=2650117688&idx=1&sn=d6c73f9f04d0 ...
- Android中的数据存储
Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedP ...
- Android五种数据存储方式
android 五种数据存储 :SharePreferences.SQLite.Contert Provider.File.网络存储 Android系统提供了四种存储数据方式.分别为:SharePre ...
- Android下的数据存储与訪问 --- 以文件的形式
Android下的数据存储与訪问 --- 以文件的形式 1.1 储存文件存放在手机内存中: // *** 储存数据到 /data/data/包名/files/jxn.txt文件里 String dat ...
随机推荐
- 检查PHP文件中是否含有bom的PHP函数
<?php /*检测并清除BOM*/ if(isset($_GET['dir'])){ $basedir=$_GET['dir']; }else{ $basedir = '.'; } $auto ...
- Windows下安装MongoDB
项目当中用到MongoDB最为NoSQL数据库,运行的平台为 Windows Server 2008,下面是MongoDB的安装过程笔记: 1.下载软件 官方下载地址:http://www.mongo ...
- 【Android学习】android:layout_weight的用法实例
对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...
- 逻辑思维面试题-java后端面试-遁地龙卷风
(-1)写在前面 最近参加了一次面试,对笔试题很感兴趣,就回来百度一下.通过对这些题目的思考让我想起了建模中的关联,感觉这些题如果没接触就是从0到1,考验逻辑思维的话从1到100会更好,并且编程简易模 ...
- 详细解读Android中的搜索框—— SearchView
以前总是自己写的 今天看看别人做的 本篇讲的是如何用searchView实现搜索框,其实原理和之前的没啥差别,也算是个复习吧. 一.Manifest.xml 这里我用一个activity进行信息的输入 ...
- 设置TextView按下时变换文字颜色
在res中建立一个color文件夹,在其中新建一个xml(这里为text_color.xml): <selector xmlns:android="http://schemas.and ...
- (备忘)自定义viewgroup与点击分发事件
public class ScoreButton extends ViewGroup 在类中重写onTouchEvent方法 @Override public boolean onTouchEvent ...
- SQL Server 2008 R2 企业版/开发版/标准版(中英文下载,带序列号)
一. 简体中文 1. SQL Server 2008 R2 Developer (x86, x64, ia64) – DVD (Chinese-Simplified) File Name: cn_sq ...
- js学习笔记---事件代理
事件机制可以分为捕获型和冒泡型.捕获型是事件由父级元素(DOM)传递到子元素.冒泡型正好相反.事件机制默认为冒泡型.事件机制可以通过参数指定. 事件委托可以将我们绑定在document上的事件自动绑定 ...
- 1.GoldenGate 简单的一对一配置
一,软件安装 源端和目标端均执行(只要修改相应的目录) 1.上传软件,放到ogg的安装目录,并解压 mkdir /home/oracle/ogg unzip ogg112101_fbo_g ...