android中常用的读取文件的用法如下
1. 从resource的raw中读取文件数据:
String res = "";
try{
//得到资源中的Raw数据流
InputStream in = getResources().openRawResource(R.raw.test);
//得到数据的大小
int length = in.available();
byte [] buffer = new byte[length];
//读取数据
in.read(buffer);
//依test.txt的编码类型选择合适的编码,如果不调整会乱码
res = EncodingUtils.getString(buffer, "BIG5");
//关闭
in.close();
}catch(Exception e){
e.printStackTrace();
}
2. 从resource的asset中读取文件数据
String fileName = "test.txt"; //文件名字
String res="";
try{
//得到资源中的asset数据流
InputStream in = getResources().getAssets().open(fileName);
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
in.close();
res = EncodingUtils.getString(buffer, "UTF-8");
}catch(Exception e){
e.printStackTrace();
}
3. 读写/data/data/<应用程序名>目录上的文件:
//写数据
public void writeFile(String fileName,String writestr) throws IOException{
try{
FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
byte [] bytes = writestr.getBytes();
fout.write(bytes);
fout.close();
}catch(Exception e){
e.printStackTrace();
}
//读数据
public String readFile(String fileName) throws IOException{
String res="";
try{
FileInputStream fin = new FileInputStream(jsonFlie);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}catch(Exception e){
e.printStackTrace();
}
return res;
}
4. 读写SD卡中的文件。也就是/mnt/sdcard/目录下面的文件
//写数据到SD中的文件
public void writeFileSdcardFile(String fileName,String write_str) throws IOException{
try{
FileOutputStream fout = new FileOutputStream(fileName);
byte [] bytes = write_str.getBytes();
fout.write(bytes);
fout.close();
}catch(Exception e){
e.printStackTrace();
}
}
//读SD中的文件
public String readFileSdcardFile(String fileName) throws IOException{
String res="";
try{
FileInputStream fin = new FileInputStream(fileName);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}catch(Exception e){
e.printStackTrace();
}
return res;
}
5. 使用File类进行文件的读写:
//读文件
public String readSDFile(String fileName) throws IOException {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
int length = fis.available();
byte [] buffer = new byte[length];
fis.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fis.close();
return res;
}
//写文件
public void writeSDFile(String fileName, String write_str) throws IOException{
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
byte [] bytes = write_str.getBytes();
fos.write(bytes);
fos.close();
}
6. [代码]五、另外,File类还有下面一些常用的操作:
String Name = File.getName(); //获得文件或文件夹的名称:
String parentPath = File.getParent(); //获得文件或文件夹的父目录
String path = File.getAbsoultePath();//绝对路经
String path = File.getPath();//相对路经
File.createNewFile();//建立文件
File.mkDir(); //建立文件夹
File.isDirectory(); //判断是文件或文件夹
File[] files = File.listFiles(); //列出文件夹下的所有文件和文件夹名
File.renameTo(dest); //修改文件夹和文件名
File.delete(); //删除文件夹或文件
1、apk中有两种资源文件,使用两种不同的方式进行打开使用。
raw使用InputStream in = getResources().openRawResource(R.raw.test);
asset使用InputStream in = getResources().getAssets().open(fileName);
这些数据只能读取,不能写入。更重要的是该目录下的文件大小不能超过1M。
同时,需要注意的是,在使用InputStream的时候需要在函数名称后加上throws IOException。
2、SD卡中的文件使用FileInputStream和FileOutputStream进行文件的操作。
3、存放在数据区(/data/data/..)的文件只能使用openFileOutput和openFileInput进行操作。
注意不能使用FileInputStream和FileOutputStream进行文件的操作。
4、RandomAccessFile类仅限于文件的操作,不能访问其他IO设备。它可以跳转到文件的任意位置,从当前位置开始读写。
摘自 T9Team
android中常用的读取文件的用法如下的更多相关文章
- C#中StreamReader类读取文件使用示例
C#中StreamReader类读取文件使用示例 1.需要导入的命名空间是:System.IO; 2.操作的是字符,所以打开的是文本文件. 常用属性: CurrentEncoding:对象正在使用 ...
- Android下使用InputStream读取文件
在Android下使用InputStream读取文件. 如果不是从头开始读取文件,使用skip 后 在读取文件 使用read读取的长度为-1会获取不到数据. 换成RandomAccessFile 使用 ...
- Android中Cursor类的概念和用法[转]
首页 > 程序开发 > 移动开发 > Android > 正文 Android中Cursor类的概念和用法 2011-09-07 0个评论 收藏 ...
- Android中使用SDcard进行文件的读取
来自:http://www.cnblogs.com/greatverve/archive/2012/01/13/android-SDcard.html 平时我们需要在手机上面存储想音频,视频等等的大文 ...
- Android中对Log日志文件的分析[转]
一,Bug出现了, 需要“干掉”它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只要你会看bug, a ...
- C#中常用的读取xml的几种方法(转)
本文完全来源于http://blog.csdn.net/tiemufeng1122/article/details/6723764,仅作个人学习之用. XML文件是一种常用的文件格式,例如WinFor ...
- Android中常用控件及属性
在之前的博客为大家带来了很多关于Android和jsp的介绍,本篇将为大家带来,关于Andriod中常用控件及属性的使用方法,目的方便大家遗忘时,及时复习参考.好了废话不多讲,现在开始我们本篇内容的介 ...
- Android中常用布局单位
Android在UI布局时经常用到一些单位,对单位混用直接会影响UI的显示,要想正确的在布局中使用每种单位就必须先真正的熟悉它. UI显示效果的影响因素:屏幕尺寸.屏幕密度.分辨率:而android手 ...
- 如何正确的在Android中存储特定应用文件
原文地址:How to Correctly Store App-Specific Files in Android Christophe Versieux (Waza_be)发表了一篇rant abo ...
随机推荐
- iOS开发 - 兼容iOS 10
1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大改重构,这让开发者也体会到UserN ...
- ASP.NET MVC Bootstrap极速开发框架
前言 每次新开发项目都要从头开始设计?有木有一个通用的快速开发框架?并且得是ASP.NET MVC And Bootstrap?数据库不要手工创建?框架对未来业务支持的扩展性好?这么简单的功能还需要 ...
- MongoDB使用汇总贴
金天:学习一个新东西,就要持有拥抱的心态,如果固守在自己先前的概念体系,就会有举步维艰的感觉.应用mongodb(NoSQL)开发,首先要打破原先的关系思维.范式思维. 本文作为使用mongodb一路 ...
- Java Security:keytool工具使用说明
Keytool用法说明 Keytool是一个key与cert的管理工具.使用keytool可以管理public key.private key,以及与key之相关的certificate. 1.com ...
- iBatis 中 Like 的写法实现模糊查询
iBatis 开发指南告诉我们,当 Person 对象的 name 属性不为 null 时启用 name 查询条件在映射文件 person.xml 中的配置为 <select id=" ...
- 0004 plsql的安装
PL/SQL是Oracle开发的主要工具,安装很简单,在官网 “https://www.allroundautomations.com/plsqldev.html” 选择合适的版本,也有多国语言包,下 ...
- linux权限补充:rwt rwT rws rwS 特殊权限
众所周知,Linux的文件权限如: 777:666等,其实只要在相应的文件上加上UID的权限,就可以用到加权限人的身份去运行这个文件.所以我们只需要将bash复制出来到另一个地方,然后用root加上U ...
- 简易的IOS位置定位服务
有时一些小的需求,其实只是需要得知当前IOS APP使用的地点,有些只是想精确到城市级别,并不需要任何地图. 有了以下的简易实现: @interface MainViewController ()&l ...
- 关于mapreduce.map.java.opts
a) Update the property in relevant mapred-site.xml(from where client load the config). b) Import t ...
- [转]基于display:table的CSS布局
当IE8发布时,它将支持很多新的CSS display属性值,包括与表格相关的属性值:table.table-row和table-cell,它也是最后一款支持这些属性值的主流浏览器.它标志着复杂CSS ...