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. NSString 字符串替换

    NSString * oneScale=@"@ddd"; NSLog(@"%@",[oneScale stringByReplacingOccurrencesO ...

  2. 关于PHP的curl开启问题

    今天在做一个新浪登录功能,新浪提供的PHP SDK里需要用到curl_init()函数,在调试的时候报找不到curl_init()的方法. 经搜索得知需要开启curl的php扩展,那curl又是什么呢 ...

  3. MAC OS 系统使用心得

    1.Windows快捷键在 mac os 里怎么调用 今天用teamviewer链接我在公司的电脑.我想调试程序,我程序默认F5是启动调试,但在mac os里,F5是调节屏幕亮度的. 这时候遇到快捷键 ...

  4. ArrayList

    各种原因,前两年做C语言去了,现在重新做JAVA, 感觉自己基础很不扎实,要好好学习啦, 先从简单的开始~ 以下内容基于jdk1.7.0_79源码: 什么是ArrayList 可以简单的认为是一个动态 ...

  5. Git从零开始怎么学?

    最近,公司项目代码版本库管理,全部从svn 迁移到Git 最近了解了一段时间 近期会把整个Git使用过程陆续分享 如何从零开始使用Git

  6. MapReduce二次排序

    默认情况下,Map 输出的结果会对 Key 进行默认的排序,但是有时候需要对 Key 排序的同时再对 Value 进行排序,这时候就要用到二次排序了.下面让我们来介绍一下什么是二次排序. 二次排序原理 ...

  7. 看看Parallel中高度封装的三个方法,Invoke,For和ForEach

    说到.net中的并行编程,也许你的第一反应就是Task,确实Task是一个非常灵活的用于并行编程的一个专用类,不可否认越灵活的东西用起来就越 复杂,高度封装的东西用起来很简单,但是缺失了灵活性,这篇我 ...

  8. jquery.validate remote的用法

    1,远程返回数据时,一定要返回"true"或者"false",否则就是永远就是验证不通过. 2,remote有两种方式,如下就介绍remote与PHP间的验证( ...

  9. spring项目的 context root 修改之后,导致 WebApplicationContext 初始化两次的解决方法

    修改了 spring web 项目的 context root 为 / 之后,在启动项目时,会导致 WebApplicationContext  初始化两次,下面是其初始化日志: 第一次初始化: 四月 ...

  10. MySQL创建和修改数据库语法

    1.创建数据库语法: CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name    [create_specification [, create_spe ...