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 ...
随机推荐
- 关于Linux与windows传递文件乱码问题
linux下一般是采用utf-8的编码,而我们在windows上编辑文件时是gb2312的编码.所以导致中文编码会乱码.要更正这个问题实际上很简单只要把文件转换成utf-8编码格式然后再导入就ok啦. ...
- 1.8 基础知识——GP2.7 识别和卷入干系人(Stakeholder) & GP2.9 质量保证(QA)
GP2.7 识别和卷入干系人(Stakeholder) GP2.7 Identify and involve the relevant stakeholders of XXX process as p ...
- Linux PHP5.3升级PHP5.5.33 (CentOS)
由于要使用了laravel5.1,php要升级到5.5以上.具体环境是Aliyun Cent OS 7.0.由于阿里的yum源lastest只有5.4,laravel5.1必须php5.5,加了几个网 ...
- oracle 分析函数的使用(1)
LISTAGG(columnName,'拼接符') WITHIN GROUP(ORDER BY clause) --order by 子句决定拼接内容的顺序 LISTAGG(columnName,'' ...
- Chrome浏览器二维码生成插件
猛击就可以使用啦->>>猛击使用 源码如下: 源码打包 源码: jquery-2.1.3.min.js jquery.qrcode.min.js https://gith ...
- tcp选项TCP_DEFER_ACCEPT
tcp选项TCP_DEFER_ACCEPT http://blog.chinaunix.net/uid-23207633-id-274317.html 之前在项目测试的时候,如果第三次握手发完裸ack ...
- Java入门(二)——果然断更的都是要受惩罚的。。。
断更了一个多月,阅读量立马从100+跌落至10-,虽说不是很看重这个,毕竟只是当这个是自己的学习笔记,但有人看,有人评论,有人认同和批评的感觉还是很巴适的,尤其以前有过却又被剥夺的,惨兮兮的. 好好写 ...
- WEB安全--逻辑漏洞
业务逻辑问题是一种设计缺陷.逻辑缺陷表现为设计者或开发者在思考过程中做出的特殊假设存在明显或隐含的错误.精明的攻击者会特别注意目标应用程序采用的逻辑方式,设法了解设计者与开发者做出的可能假设,然后考虑 ...
- SpringMVC总结帖
SpringMVC是基于MVC设计理念的一款优秀的Web框架,是目前最流行的MVC框架之一,SpringMVC通过一套注解,让POPJ成为处理请求的控制器,而无需实现任何接口,然后使用实现接口的控制器 ...
- CKEDITOR最新版不能上传图片的解决
文献:http://bbs.csdn.net/topics/390883077 代码例子:http://download.csdn.net/download/itmyhome/7851265 1.原先 ...