Android读取asserts和raw文件夹下的文件

经常需要用到读取“/res/raw”和"/asserts"文件夹下的文件,索性写成工具类方便以后使用。

一、raw文件夹下的文件操作工具类:

 /**
* raw文件夹下的文件处理工具类
*
* */
public class RawFileUtils {
private RawFileUtils( ){ } /**
* 读取raw文件夹下的文件
* @param resourceId raw文件夹下的文件资源ID
* @return 文件内容
*
* */
public static String readFileFromRaw(Context context, int resourceId) {
if( null == context || resourceId < 0 ){
return null;
} String result = null;
try {
InputStream inputStream = context.getResources().openRawResource( resourceId );
// 获取文件的字节数
int length = inputStream.available();
// 创建byte数组
byte[] buffer = new byte[length];
// 将文件中的数据读到byte数组中
inputStream.read(buffer);
result = EncodingUtils.getString(buffer, "utf-8");
} catch (Exception e) {
e.printStackTrace();
} return result;
}
}

二、asserts文件夹下的文件操作工具类:

 /**
* asserts文件处理
*
* */
public class AssertsFileUtils {
private AssertsFileUtils( ){ } /**
* 读取asserts目录下的文件
* @param fileName eg:"updatelog.txt"
* @return 对应文件的内容
*
* */
public static String readFileFromAssets(Context context, String fileName) throws IOException, IllegalArgumentException {
if (null == context || TextUtils.isEmpty( fileName )){
throw new IllegalArgumentException( "bad arguments!" );
} AssetManager assetManager = context.getAssets();
InputStream input = assetManager.open(fileName);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = input.read(buffer)) != -1) {
output.write(buffer, 0, length);
}
output.close();
input.close(); return output.toString();
} /**
* 列出Asserts文件夹下的所有文件
* @return asserts目录下的文件名列表
*
* */
public static List<String> getAssertsFiles( Context context ) throws IllegalArgumentException{
if( null == context ){
throw new IllegalArgumentException( "bad arguments!" );
} AssetManager assetManager = context.getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
e.printStackTrace( );
} return ( null == files )?null:Arrays.asList( files );
}
}

三、实例

 public class MyActivity extends Activity{

     public static final String ENCODING = "UTF-8";
TextView tv1;
TextView tv2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView)findViewById(R.id.tv1);
tv1.setTextColor(Color.RED);
tv1.setTextSize(15.0f);
tv2 = (TextView)findViewById(R.id.tv2);
tv2.setTextColor(Color.RED);
tv2.setTextSize(15.0f);
tv1.setText(getFromRaw());
tv2.setText(getFromAssets("test2.txt"));
} //从resources中的raw 文件夹中获取文件并读取数据
public String getFromRaw(){
String result = "";
try {
InputStream in = getResources().openRawResource(R.raw.test1);
//获取文件的字节数
int lenght = in.available();
//创建byte数组
byte[] buffer = new byte[lenght];
//将文件中的数据读到byte数组中
in.read(buffer);
result = EncodingUtils.getString(buffer, ENCODING);
} catch (Exception e) {
e.printStackTrace();
}
return result;
} //从assets 文件夹中获取文件并读取数据
public String getFromAssets(String fileName){
String result = "";
try {
InputStream in = getResources().getAssets().open(fileName);
//获取文件的字节数
int lenght = in.available();
//创建byte数组
byte[] buffer = new byte[lenght];
//将文件中的数据读到byte数组中
in.read(buffer);
result = EncodingUtils.getString(buffer, ENCODING);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
参考:http://blog.csdn.net/ekeuy/article/details/39479201

Android读取asserts和raw文件夹下的文件的更多相关文章

  1. java读取文件夹下所有文件并替换文件每一行中指定的字符串

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.I ...

  2. 下面的程序段创建了BufferedReader类的对象in,以便读取本机c盘my文件夹下的文件1.txt。File构造函数中正确的路径和文件名的表示是( )。

    下面的程序段创建了BufferedReader类的对象in,以便读取本机c盘my文件夹下的文件1.txt.File构造函数中正确的路径和文件名的表示是(    ). ./表示当前项目的路径../表示当 ...

  3. C# 读取指定文件夹下所有文件

    #region 读取文件 //返回指定目录中的文件的名称(绝对路径) string[] files = System.IO.Directory.GetFiles(@"D:\Test" ...

  4. cocos2d-x:读取指定文件夹下的文件名称+解决中文乱码(win32下有效)

    援引:http://blog.csdn.net/zhanghefu/article/details/21284323 http://blog.csdn.net/cxf7394373/article/d ...

  5. java笔试题: ——将e:/source文件夹下的文件打个zip包后拷贝到f:/文件夹下面

    将e:/source文件夹下的文件打个zip包后拷贝到f:/文件夹下面 import java.io.*; import java.util.zip.ZipEntry; import java.uti ...

  6. 利用shell脚本或者php移动某个文件夹下的文件到各自的日期组成的目录下

    背景是这样的:网站一开始访问量比较小,大家就把所有的图片文件上传到一个目录下(比如是/data/images/).后来访问量大了,图片也多了,这样就影响读取效率.所以有个这样的需求,把这些个图片文件移 ...

  7. C#遍历文件夹下所有文件

    FolderForm.cs的代码如下: using System; using System.Collections.Generic; using System.Diagnostics; using ...

  8. python 替换 文件夹下的 文件名称 及 文件内容

    示例效果: 1.替换某文件夹下的 文件夹及子文件夹 的名称 由OldStrDir 变为 NewStrDir: 2.替换某文件夹下的 文件夹及子文件夹 下 所有的文件的名称 由OldStrFile 变为 ...

  9. 【转发】du命令 实现Linux 某个文件夹下的文件按大小排序

    1. df -lh 2. du -s /usr/* | sort -rn这是按字节排序 3. du -sh /usr/* | sort -rn这是按兆(M)来排序 4.选出排在前面的10个du -s ...

随机推荐

  1. flask-基础知识

    Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预处理,然后 ...

  2. php 内存泄漏

    所谓内存泄漏是指进称在执行过程中,内存的占有率逐步升高,不释放, 系统所拥有的可用内存越来越少的现象. php-fpm耗光内存,不释放,就是所谓的内存泄漏,内存泄漏对长期运行的程序有威胁,所以应该定期 ...

  3. appcmd应用

    appcmd资料: http://www.jb51.net/article/36024.htm 官方文档:https://docs.microsoft.com/zh-cn/iis/get-starte ...

  4. 和为定值的m个数

    和为定值的m个数 np 问题(时间复杂度o(2**n)) code1: 暴力递归 解决 只有正数的情况 class Solution { private List<List<Integer ...

  5. 236. Lowest Common Ancestor of a Binary Tree(最低公共祖先,难理解)

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  6. PAT 天梯赛 L1-036. A乘以B 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-036 AC代码 #include <iostream> #include <cstdio&g ...

  7. C++中的内存区[译文]

    C++ 中的内存区 Const Data: The const data area stores string literals and other data whose values are kno ...

  8. React Native之持久化存储(AsyncStorage、react-native-storage)的使用

    AsyncStorage是一个简单的.异步的.持久化的Key-Value存储系统,它对于App来说是全局性的.这是官网上对它的介绍.可以知道,这个asyncstorage也是以键值对的形式进行存储数据 ...

  9. 高亮显示QSS文件

    转[作者:一去丶二三里 博客地址:http://blog.csdn.net/liang19890820] 简述 语法高亮是文本编辑器用来显示文本的,特别是源代码,根据不同的类别来用不同的颜色和字体显示 ...

  10. Linux系统下Git操作命令整理

    1.显示当前的配置信息 git config --list 2. 创建repo从别的地方获取 git clone git://git.kernel.org/pub/scm/git/git.git 自己 ...