//读取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工具类
最新推荐文章于 2022-11-23 18:17:30 发布