来自: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" />

最新文章

  1. ubuntu12.04server下red5-1.0.0RC1的部署
  2. [poj1200]Crazy Search(hash)
  3. lucene索引文件大小优化小结
  4. CSS布局:Float布局过程与老生常谈的三栏布局
  5. 锋利的jQuery-2--一个显示和隐藏的例子,主要看写法
  6. swift 委托代理传值
  7. 如何使用SC命令添加删除服务
  8. 配置处理结果result
  9. TabHost+RadioGroup搭建基础布局
  10. Spring bean中的properties元素内的name 和 ref都代表什么意思啊?
  11. Terminal,git,vim常用命令整理以及删除本地git仓库
  12. 用Java实现AES加密
  13. Zookeeper权限acl,acl的构成 scheme与id
  14. Oracle11g自带的SQL_developer无法打开
  15. class字节码结构(二)(访问标志、类索引、父类索引、接口索引集合)
  16. 文本情感分类:分词 OR 不分词(3)
  17. {Notes}{LaTeX}{enumerate}
  18. centos6.5安装apache2
  19. 在阿里云上遇见更好的Oracle(四)
  20. zabbix监控Mariadb数据库

热门文章

  1. Centos用yum升级mysql到(5.5.37)
  2. BI项目需求分析书-模板
  3. Python使用libsvm的“ImportError: No module named svmutil”问题
  4. 贴近用户体验的jQuery日期选择插件
  5. [转]VS2005/2008过期之后简单实用的升级方法
  6. C# 5.0 新特性——Async和Await使异步编程更简单
  7. [Python] Search navigation in Pycharm
  8. 还在抱怨JS文件里没有智能提示吗, VS10以及以上都可以 .NET
  9. Node.js爬虫抓取数据 -- HTML 实体编码处理办法
  10. WinForm界面开发之布局控件&quot;WeifenLuo.WinFormsUI.Docking&quot;的使用