Android读取asserts和raw文件夹下的文件
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文件夹下的文件的更多相关文章
- java读取文件夹下所有文件并替换文件每一行中指定的字符串
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.I ...
- 下面的程序段创建了BufferedReader类的对象in,以便读取本机c盘my文件夹下的文件1.txt。File构造函数中正确的路径和文件名的表示是( )。
下面的程序段创建了BufferedReader类的对象in,以便读取本机c盘my文件夹下的文件1.txt.File构造函数中正确的路径和文件名的表示是( ). ./表示当前项目的路径../表示当 ...
- C# 读取指定文件夹下所有文件
#region 读取文件 //返回指定目录中的文件的名称(绝对路径) string[] files = System.IO.Directory.GetFiles(@"D:\Test" ...
- cocos2d-x:读取指定文件夹下的文件名称+解决中文乱码(win32下有效)
援引:http://blog.csdn.net/zhanghefu/article/details/21284323 http://blog.csdn.net/cxf7394373/article/d ...
- java笔试题: ——将e:/source文件夹下的文件打个zip包后拷贝到f:/文件夹下面
将e:/source文件夹下的文件打个zip包后拷贝到f:/文件夹下面 import java.io.*; import java.util.zip.ZipEntry; import java.uti ...
- 利用shell脚本或者php移动某个文件夹下的文件到各自的日期组成的目录下
背景是这样的:网站一开始访问量比较小,大家就把所有的图片文件上传到一个目录下(比如是/data/images/).后来访问量大了,图片也多了,这样就影响读取效率.所以有个这样的需求,把这些个图片文件移 ...
- C#遍历文件夹下所有文件
FolderForm.cs的代码如下: using System; using System.Collections.Generic; using System.Diagnostics; using ...
- python 替换 文件夹下的 文件名称 及 文件内容
示例效果: 1.替换某文件夹下的 文件夹及子文件夹 的名称 由OldStrDir 变为 NewStrDir: 2.替换某文件夹下的 文件夹及子文件夹 下 所有的文件的名称 由OldStrFile 变为 ...
- 【转发】du命令 实现Linux 某个文件夹下的文件按大小排序
1. df -lh 2. du -s /usr/* | sort -rn这是按字节排序 3. du -sh /usr/* | sort -rn这是按兆(M)来排序 4.选出排在前面的10个du -s ...
随机推荐
- Java io流详解二
原文地址https://www.cnblogs.com/xll1025/p/6418766.html 一.IO流概述 概述: IO流简单来说就是Input和Output流,IO流主要是用来处理设备之间 ...
- python全栈开发从入门到放弃之列表的内置方法
1.列表切片 l=['a','b','c','d','e','f'] print(l[1:5]) # 根据索引号来切片,但顾头不顾尾 ['b', 'c', 'd', 'e'] print(l[1:5: ...
- Delphi 正则表达式语法(2): 或者与重复
Delphi 正则表达式语法(2): 或者与重复 // | 号的使用, | 是或者的意思 var reg: TPerlRegEx; begin reg := TPerlRegEx.Create ...
- CSS 中文字体 Unicode 编码表
CSS 中文字体 Unicode 编码表 在 CSS 中设置字体名称,直接写中文是可以的.但是在文件编码(GB2312.UTF-8 等)不匹配时会产生乱码的错误. 为此,在 CSS 直接使用 Unic ...
- $微信小程序开发实践点滴——Bmob常用API的使用
Bmob后端云官网:http://www.bmob.cn/ Bmob后端云微信小程序开发文档:http://docs.bmob.cn/data/wechatApp/b_developdoc/doc/i ...
- pc端html页面到移动端等比缩放
head标签里面加这个<meta name="viewport" content="user-scalable=yes">
- Linux 搭建 SVN
一.yum 安装 subversion yum -y install subversion 二.创建svn版本库所在路径(建议放在opt.usr.home下) mkdir -p /usr/local/ ...
- Linux下的查找命令which、whereis、locate、find(6/20)
Linux下查找相关命令主要有以下4个:which.whereis.locate.find. (1)which [-a] cmdname1 cmdname2 ...... 命令参数: -n ...
- 使用Linq to XML 修改app.config
使用其他的方法修改app.config无效.而且修改的是*.vshost.exe.Config,程序运行时正常,关闭之后就还是原来的值. Configuration configuration = C ...
- resin服务一直不停重启
resin服务不断重启. 原因为resin配置文件使用域名.需要到服务上绑定一下域名.