1 import android.content.Context; 2 import android.content.res.AssetManager; 3 import android.graphics.Bitmap; 4 import android.graphics.BitmapFactory; 5 6 import java.io.BufferedReader; 7 import java.io.ByteArrayOutputStream; 8 import java.io.IOException; 9 import java.io.InputStream; 10 import java.io.InputStreamReader; 11 12 13 public class LocalFileUtils { 14 15 /** 16 * 获取Asset下文本内容 17 * @param context 18 * @param str 19 * @return 20 */ 21 public final static String getStringFormAsset(Context context, String str) { 22 BufferedReader in = null; 23 try { 24 in = new BufferedReader(new InputStreamReader(context.getAssets().open(str))); 25 String line; 26 StringBuilder buffer = new StringBuilder(); 27 while ((line = in.readLine()) != null) { 28 buffer.append(line).append(‘\n‘); 29 } 30 return buffer.toString(); 31 } catch (IOException e) { 32 e.printStackTrace(); 33 return ""; 34 } finally { 35 if (in != null) { 36 try { 37 in.close(); 38 in = null; 39 } catch (IOException e) { 40 e.printStackTrace(); 41 } 42 } 43 } 44 } 45 46 /** 47 * @description 从Assets中读取图片 48 * 49 * @param context 50 * @param fileName 51 * @return 图片 52 * @date 2015-6-11 15:00:55 53 */ 54 public static Bitmap getImageFromAssetsFile(Context context, String fileName) { 55 Bitmap image = null; 56 AssetManager am = context.getAssets(); 57 InputStream is = null; 58 try { 59 is = am.open(fileName); 60 image = BitmapFactory.decodeStream(is); 61 return image; 62 } catch (IOException e) { 63 e.printStackTrace(); 64 return image; 65 } finally { 66 if(is != null) { 67 try { 68 is.close(); 69 is = null; 70 } catch (IOException e) { 71 e.printStackTrace(); 72 } 73 } 74 } 75 } 76 77 /** 78 * 获取Raw下文本内容 79 * @param context 80 * @param rawId 81 * @return 82 */ 83 public final static String getStringFormRaw(Context context, int rawId) { 84 ByteArrayOutputStream baos = null; 85 InputStream in = context.getResources().openRawResource(rawId); 86 try { 87 baos = new ByteArrayOutputStream(); 88 byte[] buffer = new byte[1024]; 89 int len = 0; 90 while ((len = in.read(buffer)) != -1) { 91 baos.write(buffer, 0, len); 92 } 93 baos.close(); 94 return baos.toString(); 95 } catch (Exception e) { 96 e.printStackTrace(); 97 return ""; 98 } finally { 99 if (baos != null) { 100 try { 101 baos.close(); 102 baos = null; 103 } catch (IOException e) { 104 e.printStackTrace(); 105 } 106 } 107 } 108 } 109 110 }
1 long startTime4 = System.currentTimeMillis(); 2 String my_json = LocalFileUtils.getStringFormAsset(this, "testbean1.json"); 3 for (int n = 0; n < 100000; n++) { 4 // 使用JSON 操作 工具将JSON字符串封装到实体类 5 TestBean1 toBean = my_gson.fromJson(my_json, my_type); //JsonTool.toBean(json, TestBean1.class); 6 System.out.println(toBean); 7 } 8 long endTime4 = System.currentTimeMillis() - startTime4; 9 Log.i(TAG, "gson....." + endTime4);
时间: 2024-11-05 15:58:06