android 数据文件存取至储存卡
来自: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 数据文件存取至储存卡的更多相关文章
- Android数据存储之SQLCipher数据库加密
前言: 最近研究了Android Sqlite数据库(文章地址:Android数据存储之Sqlite的介绍及使用)以及ContentProvider程序间数据共享(Android探索之ContentP ...
- Android数据存储之GreenDao 3.0 详解
前言: 今天一大早收到GreenDao 3.0 正式发布的消息,自从2014年接触GreenDao至今,项目中一直使用GreenDao框架处理数据库操作,本人使用数据库路线 Sqlite----> ...
- Android数据存储方式--SharedPreferences
Android数据存储方式有如下四种:SharedPreferences.存储到文件.SQLite数据库.内容提供者(Content provider).存储到网络服务器. 本文主要介绍一下Share ...
- Android数据存储-通过SharedPreferences实现记住密码的操作
在Android中登陆中,为了实现用户的方便,往往需要根据用户的需要进行记住密码的操作,所以,在Android数据存储中SharedPreferences恰恰可以实现这一点 下面,小编将带领大家通过S ...
- Android数据缓存(转)
Android数据缓存 1.http://blog.csdn.net/lnb333666/article/details/8460159 2.https://github.com/Trinea/a ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览
Android数据的四种存储方式SharedPreferences.SQLite.Content Provider和File (一) —— 总览 作为一个完成的应用程序,数据存储操作是必不可少的. ...
- Android数据存储(一)----SharedPreferences详解
一.Android数据的存储方式: Android系统一共提供了四种数据存储方式.分别是:SharePreference.SQLite.Content Provider和File:此外还有一种网络存储 ...
- 10、Android数据存储
课程目标: 掌握Android中数据存储的几种方式 熟练使用PreferenceActivity&PreferenceScreen做专业的Setting功能 熟练使用SQLite3来存储数据 ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...
随机推荐
- (oracle/mysql)启停,导入导出,DB字符集
==================================================================================== DB启动停止 ======== ...
- netty ByteBuf分析
1.Heap Buffer(堆缓冲区) 2.Direct Buffer(直接缓冲区) 3.Composite Buffer(复合缓冲区) 4.PooledByteBuf 池缓冲 readerInex ...
- [iOS]关于状态栏(UIStatusBar)的若干问题
版本: OS X 10.10.5 Xcode 6.4(6E35b) iOS >= 7 一.概述 状态栏(UIStatusBar)指iPhone/iPad/iPod屏幕顶部用于显示网络.时间和电量 ...
- Unity 资源管理与更新
当你在 工程目录下的 Asset 文件夹中放置一个文件时(电脑上的文件夹,不是 Unity 界面中的文件夹),Unity会自动检测到一个新的文件被添加(Unity会不停地检查Assets文件夹中的内容 ...
- SQL Server技术问题之索引优缺点
索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息. 优点: 正确的索引会大大提高数据查询.对结果排序.分组的操作效率. 缺点: 1.存储空间,每个索引都要空间 ...
- 分析SIX锁和锁分区导致的死锁
什么是SIX锁? 官方文档锁模式中说到: 意向排他共享 (SIX):保护针对层次结构中某些(而并非所有)低层资源请求或获取的共享锁以及针对某些(而并非所有)低层资源请求或获取的意向排他锁. 顶级资源允 ...
- Gradle学习系列之四——增量式构建
在本系列的上篇文章中,我们讲到了如何读懂Gradle的语法,在本篇文章中,我们将讲到增量式地构建项目. 请通过以下方式下载本系列文章的Github示例代码: git clone https://git ...
- Scrum 项目7.0--软件工程
Sprint回顾 1.回顾组织 主题:“我们怎样才能在下个sprint中做的更好?” 时间:40分钟. 参与者:整个团队. 场所:电脑教室. 秘书:卢晓洵. 2.回顾流程 sprint总结 ...
- 循序渐进开发WinForm项目(2)--项目代码的分析
随笔背景:在很多时候,很多入门不久的朋友都会问我:我是从其他语言转到C#开发的,有没有一些基础性的资料给我们学习学习呢,你的框架感觉一下太大了,希望有个循序渐进的教程或者视频来学习就好了. 其实也许我 ...
- JS Array ECMAScript5 Methods
JavaScript 的新版本(ECMAScript 5)中,为数组新增了一些方法.这些方法包括: forEach(f [,o]): 此方法类似于for/in循环,其作用是遍历整个数组并执行函数的某些 ...