//讀取Assets目錄下讀取指定文件
public class FileUtils {
/**
* 在Assets目錄下讀取指定文件,併產生List集合
* @param context
* @param clazz:Bean類,必須包含set、get方法
* @param fileName
* @return
*/
public static <T> List<T> readDataFromAssets(Context context,
Class<T> clazz, String fileName){
InputStream is = null;
BufferedReader br = null;
List<T> data = new ArrayList<T>();
try {
is = context.getAssets().open(fileName);
br = new BufferedReader(new InputStreamReader(is, "gbk"));
String temp = null;
while ((temp = br.readLine()) != null) {
T t = clazz.newInstance();
Method[] methods = clazz.getDeclaredMethods();
for(int i = 0; i<methods.length; i++){
Method method = methods[i];
// if("setId".equals(method.getName())){
// method.invoke(t, i);
// }
if("setGiftName".equals(method.getName())){
method.invoke(t, temp);
}
if ("setPicName".equals(method.getName())) {
method.invoke(t, R.drawable.gift_lipin);
}
//==============================================
if("setParams".equals(method.getName())){
method.invoke(t, temp);
}
//==============================================
}
data.add(t);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close(is, br);
}
// Log.d("lu", data.toString());
return data;
}
public static void close(InputStream is, BufferedReader br) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
【Android】Assets工具類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.