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. 开放-封闭原则(OCP)开-闭原则 和 依赖倒转原则,单一职责原则

    单一职责原则 1.单一职责原则(SRP),就一个类而言,应该仅有一个引起它变化的原因 2.如果一个类承担的职责过多,就等于把这些职责耦合在一起,一个职责的变化可能会消弱或抑制这个类完成其他职责的能力. ...

  2. Android IPC机制之AIDL

    什么是AIDL AIDL:Android Interface Definition Language,即Android接口定义语言. Android系统中的进程之间不能共享内存,因此,需要提供一些机制 ...

  3. php示例代码使用mysql_fetch_assoc函数

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  4. .net开发中常用的第三方组件

    .net开发中常用的第三方组件 2013-05-09 09:33:32|  分类: dotnet |举报 |字号 订阅     下载LOFTER 我的照片书  |   RSS.NET.dll RSS. ...

  5. HTML和CSS高级指南——定位详解

    本文由大漠根据Shay Howe的<An Adavnced Guide to HTML & CSS>第二课<Detailed Positioning>所译,整个译文带有 ...

  6. git之一

    1.Git是什么Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.Git 是 ...

  7. ListView的监听器中OnItemClick各个参数的作用

    方法的原型如下 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){ } 后面有4个参 ...

  8. Java api 入门教程 之 JAVA的Date类与Calendar类

    在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理. 一.这里简单介绍一下Date类的使 ...

  9. nsis制作启动Tomcat服务的exe安装包教程

    一. 准备工作 下载nsis相关工具包,点击此下载 1. 安装程序:nsis-2.46-setup.exe 2. 编辑程序:cnisedit203.exe 3. 帮助文档:NSIS205帮助文档.ra ...

  10. 3.10.17 procfs示例

    /* *    wesley *    the linux module sample of procfs *     *    wesley for testing *    2013-12-23 ...