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的文件系统对数据进行临时的保存操作,并不是持久化数据,例如网络上下载某些图片.音频.视频文件等.如缓存文件将会在清理应用缓存的时候被清除,或者是应用卸载的时 ...
随机推荐
- Qt5:窗口各类位置
在Qt程序中获取窗口位置的函数有 geometry() , frameGeometry() , pos() ,x() , y()等 下面来看看这些函数的区别 还有另外两个函数 size() ...
- A. Round House
A. Round House time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Vmware克隆虚拟机后网卡eth0变eth1解决
在克隆虚拟机的过程中,发现新克隆的机器的网卡由eth0变成了eth1,然而并没有eth1的配置文件. 1.#ip a 查看当前ip地址,发现是eth1网卡 2.#ll /etc/sysconfig/n ...
- JS——基础知识(三)
1.select (1)它的选择事件是onchange (2)他的选项索引可以通过value获取,比tab选项卡要方便一点. 2.数组常用方法 (1)添加元素 push():可以向一个数组末尾添加一个 ...
- 使用eclipse和maven一步一步配置web项目
http://www.blogjava.net/kevonz/archive/2012/07/08/382542.html
- nginx+fastcgi php 使用file_get_contents、curl、fopen读取localhost本站点.php异常的情况
原文:http://www.oicto.com/nginx_fastcgi_php_file_get_contents/ 参考:http://os.51cto.com/art/201408/44920 ...
- 史上最强php生成pdf文件,html转pdf文件方法
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- [Programming WCF Services]Chapter 1. WCF Essentials - Metadata Exchange
1.HTTP-GET WCF 方式 通过Http的方式提供metadata 1.1.配置文件方式 <system.serviceModel> <services> <se ...
- [转]ASP.NET Core 1 Deploy to IIS
本文转自: http://webmodelling.com/webbits/aspnet/aspnet-deploy-iis.aspx 15 Sep 2016. This tutorial will ...
- UVa11555 - Aspen Avenue
今晚CF GYM A题,神坑.. 原题: Aspen Avenue ``Phew, that was the last one!'' exclaimed the garden helper Tim a ...