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中常用的读取文件的用法如下的更多相关文章

  1. C#中StreamReader类读取文件使用示例

    C#中StreamReader类读取文件使用示例 1.需要导入的命名空间是:System.IO; 2.操作的是字符,所以打开的是文本文件. 常用属性:   CurrentEncoding:对象正在使用 ...

  2. Android下使用InputStream读取文件

    在Android下使用InputStream读取文件. 如果不是从头开始读取文件,使用skip 后 在读取文件 使用read读取的长度为-1会获取不到数据. 换成RandomAccessFile 使用 ...

  3. Android中Cursor类的概念和用法[转]

    首页 > 程序开发 > 移动开发 > Android > 正文   Android中Cursor类的概念和用法 2011-09-07      0个评论       收藏    ...

  4. Android中使用SDcard进行文件的读取

    来自:http://www.cnblogs.com/greatverve/archive/2012/01/13/android-SDcard.html 平时我们需要在手机上面存储想音频,视频等等的大文 ...

  5. Android中对Log日志文件的分析[转]

    一,Bug出现了, 需要“干掉”它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只要你会看bug, a ...

  6. C#中常用的读取xml的几种方法(转)

    本文完全来源于http://blog.csdn.net/tiemufeng1122/article/details/6723764,仅作个人学习之用. XML文件是一种常用的文件格式,例如WinFor ...

  7. Android中常用控件及属性

    在之前的博客为大家带来了很多关于Android和jsp的介绍,本篇将为大家带来,关于Andriod中常用控件及属性的使用方法,目的方便大家遗忘时,及时复习参考.好了废话不多讲,现在开始我们本篇内容的介 ...

  8. Android中常用布局单位

    Android在UI布局时经常用到一些单位,对单位混用直接会影响UI的显示,要想正确的在布局中使用每种单位就必须先真正的熟悉它. UI显示效果的影响因素:屏幕尺寸.屏幕密度.分辨率:而android手 ...

  9. 如何正确的在Android中存储特定应用文件

    原文地址:How to Correctly Store App-Specific Files in Android Christophe Versieux (Waza_be)发表了一篇rant abo ...

随机推荐

  1. TFS2012 自动生成与部署

    思路: 每日构建,自动生成,然后从TFS提交日志中提取版本修订说明,调用打包脚本混淆并生成安装包(系统自带的太锉),最后将相关文件复制到指定网站供浏览下载.自动向测试网站发布,自动生成数据库并初始化. ...

  2. C#参考书的链接推荐

    Visual C#.NET入门与提高http://download.chinaitlab.com/soft/6330.htm 使用Visual C# 开发asp.NET入门http://downloa ...

  3. Java 往年试卷参考答案!!!

    仅供参考: 第一题: E C E A D D C A C A C A B A B C C D B C 第二题: True True False 11 12 13 14 No such file fou ...

  4. Apache安装问题:configure: error: APR not found . Please read the documentation

    Linux上安装Apache时,编译出现错误: checking for APR... no configure: error: APR not found .  Please read the do ...

  5. 使用bakefile编译C工程代码

    前言 最近有个想法,想把ineedle整体架构从头自己编写代码来实现一下,来加深对iNeedle系统的理解,同时加强Linux + C相关知识.由于iNeedle系统的庞大,只能是先把框架搭起来,根据 ...

  6. mongo数据备份及恢复脚本

    #!/bin/bashtime="$(date +"%Y.%m.%d")" id=`echo "show dbs;"|/usr/local/ ...

  7. android canvas d

    (以下转自:http://blog.csdn.net/longyi_java/article/details/6930480) 1.基本的绘制图片方法 //Bitmap:图片对象,left:偏移左边的 ...

  8. setTimeout()与setInterval()——走马灯效果

    JavaScript中的setTimeout()与setInterval()都是指延时执行某一操作. 但setInterval()指每隔指定时间执行某操作,会循环不断地执行该操作:setTimeout ...

  9. tar 解压出错

    今天下载了一个Linux内核文件,解压的时候出现了这样的错误: gzip: stdin: unexpected end of file tar: Unexpected EOF in archive t ...

  10. 在VMware上安装CentOS-6.5 minimal - 安装VMware Tools

    由于CentOS-6.5 minimal很多工具都默认没有安装,安装VMwareTools需要用到Perl,所以老伯建议先配置好网络再接着安装. 网络配置方法可以参考在VMware上安装CentOS- ...