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.****.net/ekeuy/article/details/39479201