来自:http://blog.csdn.net/jianghuiquan/article/details/8569233

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存数据(File)" /> <Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取数据(File)" /> </LinearLayout>
package com.example.yanlei.wifi;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner; public class MainActivity extends AppCompatActivity { private Button btnSave=null;
private Button btnRead=null;
private File file=null; private static final String FILENAME="data.txt"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSave=(Button)super.findViewById(R.id.save);
btnRead=(Button)super.findViewById(R.id.read); btnSave.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{ PrintStream ps=null; //判断外部存储卡是否存在
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(getApplicationContext(), "读取失败,SD存储卡不存在!", Toast.LENGTH_LONG).show();
return;
} //初始化File
String path=Environment.getExternalStorageDirectory().toString()
+File.separator
+"genwoxue"
+File.separator
+FILENAME;
file=new File(path); //如果当前文件的父文件夹不存在,则创建genwoxue文件夹
if(!file.getParentFile().exists())
file.getParentFile().mkdirs(); //写文件
try {
ps = new PrintStream(new FileOutputStream(file));
ps.println("跟我学网址:www.genwoxue.com");
ps.println("");
ps.println("电子邮件:hello@genwoxue.com");
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
ps.close(); }
Toast.makeText(getApplicationContext(), "保存成功!", Toast.LENGTH_LONG).show();
}
}); btnRead.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
StringBuffer info=new StringBuffer(); //判断外部存储卡是否存在
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(getApplicationContext(), "读取失败,SD存储卡不存在!", Toast.LENGTH_LONG).show();
return;
} //初始化File
String path=Environment.getExternalStorageDirectory().toString()
+File.separator
+"genwoxue"
+File.separator
+FILENAME;
file=new File(path); if(!file.exists()){
Toast.makeText(getApplicationContext(), "文件不存在!", Toast.LENGTH_LONG).show();
return;
} //读取文件内容
Scanner scan=null;
try {
scan=new Scanner(new FileInputStream(file));
while(scan.hasNext()){
info.append(scan.next()).append("☆☆☆\n");
}
Toast.makeText(getApplicationContext(), info.toString(), Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
scan.close();
}
}
});
}
}

权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yanlei.wifi" >
<!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

android 数据文件存取至储存卡的更多相关文章

  1. Android数据存储之SQLCipher数据库加密

    前言: 最近研究了Android Sqlite数据库(文章地址:Android数据存储之Sqlite的介绍及使用)以及ContentProvider程序间数据共享(Android探索之ContentP ...

  2. Android数据存储之GreenDao 3.0 详解

    前言: 今天一大早收到GreenDao 3.0 正式发布的消息,自从2014年接触GreenDao至今,项目中一直使用GreenDao框架处理数据库操作,本人使用数据库路线 Sqlite----> ...

  3. Android数据存储方式--SharedPreferences

    Android数据存储方式有如下四种:SharedPreferences.存储到文件.SQLite数据库.内容提供者(Content provider).存储到网络服务器. 本文主要介绍一下Share ...

  4. Android数据存储-通过SharedPreferences实现记住密码的操作

    在Android中登陆中,为了实现用户的方便,往往需要根据用户的需要进行记住密码的操作,所以,在Android数据存储中SharedPreferences恰恰可以实现这一点 下面,小编将带领大家通过S ...

  5. Android数据缓存(转)

    Android数据缓存   1.http://blog.csdn.net/lnb333666/article/details/8460159 2.https://github.com/Trinea/a ...

  6. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览

    Android数据的四种存储方式SharedPreferences.SQLite.Content Provider和File (一) —— 总览   作为一个完成的应用程序,数据存储操作是必不可少的. ...

  7. Android数据存储(一)----SharedPreferences详解

    一.Android数据的存储方式: Android系统一共提供了四种数据存储方式.分别是:SharePreference.SQLite.Content Provider和File:此外还有一种网络存储 ...

  8. 10、Android数据存储

    课程目标: 掌握Android中数据存储的几种方式 熟练使用PreferenceActivity&PreferenceScreen做专业的Setting功能 熟练使用SQLite3来存储数据 ...

  9. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences

    除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...

随机推荐

  1. Android Material Design控件学习(一)——TabLayout的用法

    前言 Google官方在14年Google I/O上推出了全新的设计语言--Material Design.一并推出了一系列实现Material Design效果的控件库--Android Desig ...

  2. 对C#对象的Shallow、Deep Cloning认识【转】

    好像园内比较多博客对 Shallow.Deep Cloning的翻译是深拷贝.浅拷贝,当时我懵了,这个叫法怎么怪怪的. 就好像看军情观察室,台湾评论员,导弹叫飞弹. 至于它们的区别,一张图就可以解释. ...

  3. Inkpad中文翻译已合并到官方项目

    今天 Steve Sprang 已合并了#100提交请求,Inkpad即将在AppStore上发布简体中文版了! 20天前因一个偶然原因启动翻译的: 当晚(周六)我想对iPad上的矢量绘图软件进行交互 ...

  4. Machine Learning : Pre-processing features

    from:http://analyticsbot.ml/2016/10/machine-learning-pre-processing-features/ Machine Learning : Pre ...

  5. 自己动手搭建 CAS(Central Authentication Service) 环境,为了单点登录(Single Sign On , 简称 SSO )

    介绍 刚刚搭建 CAS 成功了,现在记录下来,怕以后忘记,同时也给需要帮助的人.CAS 搭建需要服务端和客户端,服务端是 Java 写的,如果自己搭建则需要安装与配置 Java 环境.客户端可以是多种 ...

  6. Enjoy Android

    大趋势所迫,开始学习Android, @mark一下

  7. C#/C++ 中字节数组与int类型转换

    1.C#中int和byte[]转换: /// <summary> /// 把int32类型的数据转存到4个字节的byte数组中 /// </summary> /// <p ...

  8. [Node.js] 使用File API 异步上传文件

    原文地址:http://www.moye.me/2014/11/05/html5-filereader/ 最近在做一个网盘的项目,不出意外的涉及到大文件的上传,那么问题来了:如何实时的显示文件上传的进 ...

  9. 如何拿到国内IT巨头的Offer

    感觉写的很真实,分享一下.原文链接:http://jingyan.baidu.com/article/72ee561aa16d23e16138df3d.html 不久前,byvoid面阿里星计划的面试 ...

  10. EF封装类 增加版,增加从缓存中查找数据方法,供参考!

    EF封装类 增加版,增加从缓存中查找数据方法,供参考! 这个类是抽象类,我这里增加了需要子类验证的方法ValidateEntity,方便扩展,若想直接使用该类,可以将该类更改成静态类,里面所有的方法都 ...