二十、Android -- SDcard文件读取和保存
背景
一些东西可以存在自己定义的文件里面,这个文件可以在手机中,可以在SD卡中,在这里就主要介绍一下在SD卡中的存储和读取吧~
代码

public class save {
public static void savefile2card(Context context,String username,String password)
{
File file = null;
FileOutputStream fos = null;
try
{
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
// file = new File("/sdcard/info.txt");
file = new File(Environment.getExternalStorageDirectory(),"info.txt");
fos = new FileOutputStream(file);
fos.write((username+"!!!!"+password).getBytes());
}
else
{
Toast.makeText(context, "SD木有", Toast.LENGTH_LONG).show();
}
}
catch (Exception e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
Toast.makeText(context, "Wrong", Toast.LENGTH_LONG).show();
try
{
fos.close();
}
catch (IOException e1)
{
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
}
}

上面是存的代码,这里面用到了Environment.MEDIA_MOUNTED,查看是否挂载。

public class read {
public static Map<String,String> getSaveFile(Context context)
{
//File file =new File(context.getFilesDir(),"info.txt");
File file = new File(Environment.getExternalStorageDirectory(),"info.txt");
try
{
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String str = br.readLine();
String[] infos = str.split("!!!!");
Map<String,String> map = new HashMap<String, String>();
map.put("username",infos[0]);
map.put("password", infos[1]);
br.close();
return map;
}
catch (Exception e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
return null;
}
finally
{
}
}
}

上面是读取的代码,会存就会读了,存得进去那么就读得出来撒~
源代码:http://pan.baidu.com/s/1dD1Qx01
SDcard.zip
二十、Android -- SDcard文件读取和保存的更多相关文章
- Android -- SDcard文件读取和保存
背景 一些东西可以 ...
- JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习
JAVA之旅(二十九)--文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习 我们继续学习File 一.文件递归 我们可以来实现 ...
- JAVA之旅(二十五)——文件复制,字符流的缓冲区,BufferedWriter,BufferedReader,通过缓冲区复制文件,readLine工作原理,自定义readLine
JAVA之旅(二十五)--文件复制,字符流的缓冲区,BufferedWriter,BufferedReader,通过缓冲区复制文件,readLine工作原理,自定义readLine 我们继续IO上个篇 ...
- ComicEnhancerPro 系列教程二十:用“文件比较”看有损、无损
作者:马健邮箱:stronghorse_mj@hotmail.com 主页:http://www.comicer.com/stronghorse/ 发布:2017.07.23 教程二十:用“文件比较” ...
- Unity读取Android SDcard文件
一.添加权限 权限添加 :Player settings -- Other settings -- write permission的设置 Sdcard.这个是在Unity编辑器里打包的情况. 如果导 ...
- Spark(十二)【SparkSql中数据读取和保存】
一. 读取和保存说明 SparkSQL提供了通用的保存数据和数据加载的方式,还提供了专用的方式 读取:通用和专用 保存 保存有四种模式: 默认: error : 输出目录存在就报错 append: 向 ...
- 流操作text文件------读取、保存文档
************************************一.读取指定text文档中的内容:**************************************** 方法一. t ...
- Android NDK开发指南(二)Android.mk文件
http://www.cnblogs.com/yaozhongxiao/archive/2012/03/06/2382225.html 1. 概述 Android.mk文件是用来描述build sy ...
- java web学习总结(二十四) -------------------Servlet文件上传和下载的实现
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...
随机推荐
- Flex4 DataGrid ItemRenderer内嵌方式
Flex4 DataGrid ItemRenderer像Flex3一直内嵌ItemRenderer会报空对象引用的错误,如: <s:GridColumn dataField="titl ...
- boost 线程、互斥体、条件变量
1.任何技术都是针对特定场景设计的,也就是说,为了解决某个问题而设计的. 2.考虑下面一种场景:一个小旅馆,只有一个卫生间,有清洁人员,店主人,和旅客.卫生间用完之后,就会自动锁闭,必须取钥匙,才能进 ...
- 【转】C++ 异常
一.什么是异常处理 一句话:异常处理就是处理程序中的错误. 二.为什么需要异常处理,以及异常处理的基本思想 C++之父Bjarne Stroustrup在<The C++ Programming ...
- .NET连接SAP系统专题:SAP中新建可远程调用的RFC(二)
何谓RFC,就是一个Function,可以被非SAP系统调用,比如VB,C#,Java等.如果我们在RFC中INCLUDE了相关的业务逻辑,那么我们就可以完全操控SAP中的业务数据了.就像在TTE里, ...
- BZOJ 1295: [SCOI2009]最长距离 spfa
1295: [SCOI2009]最长距离 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1295 Description windy有一块 ...
- UVA 573 (13.08.06)
The Snail A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...
- GLSL实现Simple Displace Mapping 水仿真流体绘制 【转】
http://blog.csdn.net/a3070173/archive/2008/11/20/3342062.aspx Dislace Mapping其实就是在顶点着色器中 对顶点进行置换偏移,经 ...
- 关于View端
View--------------Request 1 URL vs n View 同一个URL可以对应多个View, HTML(通过Request请求获得) 例如SAO项目中的step1--> ...
- [AngularJS] Adding custom methods to angular.module
There are situations where you might want to add additional methods toangular.module. This is easy t ...
- Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
最近,在android中用代码动态改变某种布局(组件)的高度时,会遇到如题所示的类转换异常.上网查了一下,如下所示: These supply parameters to the parent of ...