Android的文件存储
//文件的写入
String content1 = edt_file.getText().toString();
//用于文件的写操作
FileOutputStream fos=null;
//缓冲输入流
BufferedWriter writer = null;
try {
//如果文件存在,直接打开文件,如果不存在,创建文件
//openFileOutput(String name,int mode)方法:用于向当前应用文件夹下输出文件,并返回FileOutputStream输出流。
fos = openFileOutput("test.txt", Context.MODE_PRIVATE);
//OutputStreamWriter作用是缓冲输入流与文件写操作的关联
writer = new BufferedWriter(new OutputStreamWriter(fos));
//原来BufferedWriter是缓冲输入流,意思是当你调用BufferedWriter的write方法时候。
//数据是先写入到缓冲区里,并没有直接写入到目的文件里。你必须调用BufferedWriter的flush()方法。
//这个方法会刷新一下该缓冲流,也就是会把数据写入到目的文件里。或者你可以调用BufferedWriter的close()方法,
//该方法会在关闭该输入流之前先刷新一下该缓冲流。也会把数据写入到目的文件里
writer.write(content1); //写入缓冲区
writer.flush(); //必须要写,要不然不会保存在文件中,只会在缓冲区中。
Toast.makeText(SharedPerferencesActivity.this, "写入文件完成", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(writer != null)
{
writer.close(); //关闭缓冲输入流
}
if(fos != null)
{
fos.close(); //关闭写操作
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//文件的读取
//缓冲输出流
BufferedReader reader=null;
StringBuilder sb = new StringBuilder();
try {
//openFileInput(String name)方法:用于读取当前应用文件夹下的文件,并返回FileInputStream输入流。
reader = new BufferedReader(new InputStreamReader(openFileInput("test.txt")));
String content2 = reader.readLine();//读出行数据
while(content2 != null)//循环读出数据
{
sb.append(content2);
content2 = reader.readLine();
}
edt_file.setText(sb.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(reader != null)//关闭缓冲输出流的数据
{
reader.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Raw资源的读取:
//原始资源文件位于res/raw目录下,程序部署时会将其完整地打包进apk,在程序中可通过资源ID访问。
InputStream is = getResources().openRawResource(R.raw.rawtest);
BufferedReader reader2 =null;
StringBuffer sb2 = new StringBuffer();
try {
reader2 = new BufferedReader(new InputStreamReader(is,"GBK"));
String content2 = reader2.readLine();
while(content2 != null)
{
sb2.append(content2);
content2 = reader2.readLine();
}
edt_file.setText(sb2.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(reader2 != null)
{
reader2.close();
}
if(is != null)
{
is.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//SD卡的写入
//1.判断SD状态
FileOutputStream fos1 = null;
try {
String statue = Environment.getExternalStorageState();
if(statue.equals(Environment.MEDIA_MOUNTED))
{
//SD卡可用
//Toast.makeText(SharedPerferencesActivity.this, "当前手机的SD卡可用...", Toast.LENGTH_LONG).show();
//得到路径
File dir = Environment.getExternalStorageDirectory();
//Toast.makeText(SharedPerferencesActivity.this, dir.getCanonicalPath(), Toast.LENGTH_LONG).show();
File file = new File(dir,"test.txt");
fos1 = new FileOutputStream(file);
Write(fos1);
}
else
{
Toast.makeText(SharedPerferencesActivity.this, "当前手机的SD卡有问题...", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(fos1 !=null)
{
fos1.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//SD可的读取
//1.判断SD状态
FileInputStream fis = null;
try {
String statue1 = Environment.getExternalStorageState();
if(statue1.equals(Environment.MEDIA_MOUNTED))
{
//SD卡可用
//Toast.makeText(SharedPerferencesActivity.this, "当前手机的SD卡可用...", Toast.LENGTH_LONG).show();
//InputStream
File dir2= Environment.getExternalStorageDirectory();
File file2 = new File(dir2,"test.txt");
fis = new FileInputStream(file2);
Read(fis);
}
else
{
Toast.makeText(SharedPerferencesActivity.this, "当前手机的SD卡有问题...", Toast.LENGTH_LONG).show();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(fis != null)
{
fis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Android的文件存储的更多相关文章
- Android使用文件存储数据
Android上最基本的存储数据的方式即为使用文件存储数据,使用基本的Java的FileOutStream,BufferedWriter,FileInputStream和BufferedReader即 ...
- 19.Android之文件存储方法学习
Android开发中会用到文件存储,今天来学习下. 先改下布局界面: <?xml version="1.0" encoding="utf-8"?> ...
- Android学习——文件存储
在Andriod开发中,文件存储和Java的文件存储类似.但需要注意的是,为了防止产生碎片垃圾,在创建文件时,要尽量使用系统给出的函数进行创建,这样当APP被卸载后,系统可以将这些文件统一删除掉.获取 ...
- Android File文件存储功能
1.介绍 2.使用方法 3.文件存储位置 4.java后台代码 package com.lucky.test47file; import android.support.v7.app.AppCompa ...
- android 开发-文件存储之读写sdcard
android提供对可移除的外部存储进行文件存储.在对外部sdcard进行调用的时候首先要调用Environment.getExternalStorageState()检查sdcard的可用状态.通过 ...
- android之文件存储和读取
一.权限问题 手机中存储空间分为ROM和SDcard,ROM中放着操作系统以及我们安装的APP,而sdcard中一般放置着我们APP产生的数据.当然,Android也为每个APP在ROM中创建一个数据 ...
- Android - 读取文件存储的数据
存取手机中的文件数据. 写入和读取的操作格式均为UTF-8. import java.io.File; import java.io.FileInputStream; import java.io.F ...
- Android数据存储之Android 6.0运行时权限下文件存储的思考
前言: 在我们做App开发的过程中基本上都会用到文件存储,所以文件存储对于我们来说是相当熟悉了,不过自从Android 6.0发布之后,基于运行时权限机制访问外置sdcard是需要动态申请权限,所以以 ...
- android 开发-数据存储之文件存储
android的文件存储是通过android的文件系统对数据进行临时的保存操作,并不是持久化数据,例如网络上下载某些图片.音频.视频文件等.如缓存文件将会在清理应用缓存的时候被清除,或者是应用卸载的时 ...
随机推荐
- MIME (Multipurpose Internet Mail Extensions) 是描述消息内容类型的因特网标准
http://www.alixixi.com/program/a/2010080364045.shtml MIME 消息能包含文本.图像.音频.视频以及其他应用程序专用的数据. 官方的 MIME 信息 ...
- 关于 Equal Override Overload 和 IEquatable
namespace TestEqual { class Program { static void Main(string[] args) { Point2D a = new Point2D { X ...
- HTML之禁止输入文本
一个文本框,禁止输入文本有2个方式,一个是利用readonly ,一个是利用 disabled. 那么两者虽然目的都可以达到,但是从表现上来看disabled会显得更加的直观,为什么这么说. 请看截图 ...
- Windows Server 2012如果打开网页慢下载快的话
原来Windows server 2012默认打开了ECN功能(貌似从Windows server 2008之后都默认打开),个人操作系统却没有打开,而办公室网络的确拥塞不小,造成了这种效果.好了, ...
- fn标签
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
- 转发:招聘一个靠谱的 iOS
觉得很瘦感触,因此转发:http://blog.sunnyxx.com/2015/07/04/ios-interview/ 近一年内陆续面试了不少人了,从面试者到面试官的转变让我对 iOS 招聘有了更 ...
- Ibatis 3.0 之前使用的都是2.0 3.0与2.0的内容有很大的不同
以前用过ibatis2,但是听说ibatis3有较大的性能提升,而且设计也更合理,他不兼容ibatis2.尽管ibatis3还是beta10的状态,但还是打算直接使用ibatis3.0, ibatis ...
- 通过条件注释<!--[if IE]><!-->判断浏览器
有时我们会在网站头部看到: <!--[if IE 7]> <![endif]--> 或者 <!--[if lt IE 9]> <![endif]--> ...
- java开发之提高java和mysql代码性能和质量
0.if嵌套的层数最好不要超过3层 点击(此处)折叠或打开 import java.util.HashMap; import java.util.Map; public class Qiantao { ...
- php上传zip文件在线解压文件在指定目录下,CI框架版本
我从网上找的文件php在线解压zip压缩文件 文件为jy.php可以直接执行,但是怎样将其加到CI框架中呢?? jy.php文件 <?php header("content-Type: ...