|
3 | 3 | import java.io.BufferedReader;
|
4 | 4 | import java.io.IOException;
|
5 | 5 | import java.io.InputStreamReader;
|
6 |
| - |
7 |
| -import cn.trinea.android.common.util.StringUtils; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
8 | 8 |
|
9 | 9 | import android.content.Context;
|
10 | 10 |
|
@@ -71,4 +71,61 @@ public static String geFileFromRaw(Context context, int resId) {
|
71 | 71 | return null;
|
72 | 72 | }
|
73 | 73 | }
|
| 74 | + |
| 75 | + /** |
| 76 | + * same to {@link ResourceUtils#geFileFromAssets(Context, String)}, but return type is List<String> |
| 77 | + * |
| 78 | + * @param context |
| 79 | + * @param fileName |
| 80 | + * @return |
| 81 | + */ |
| 82 | + public static List<String> geFileToListFromAssets(Context context, String fileName) { |
| 83 | + if (context == null || StringUtils.isEmpty(fileName)) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + List<String> fileContent = new ArrayList<String>(); |
| 88 | + try { |
| 89 | + InputStreamReader in = new InputStreamReader(context.getResources().getAssets().open(fileName)); |
| 90 | + BufferedReader br = new BufferedReader(in); |
| 91 | + String line; |
| 92 | + while ((line = br.readLine()) != null) { |
| 93 | + fileContent.add(line); |
| 94 | + } |
| 95 | + br.close(); |
| 96 | + return fileContent; |
| 97 | + } catch (IOException e) { |
| 98 | + e.printStackTrace(); |
| 99 | + return null; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * same to {@link ResourceUtils#geFileFromRaw(Context, int)}, but return type is List<String> |
| 105 | + * |
| 106 | + * @param context |
| 107 | + * @param resId |
| 108 | + * @return |
| 109 | + */ |
| 110 | + public static List<String> geFileToListFromRaw(Context context, int resId) { |
| 111 | + if (context == null) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + List<String> fileContent = new ArrayList<String>(); |
| 116 | + BufferedReader reader = null; |
| 117 | + try { |
| 118 | + InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId)); |
| 119 | + reader = new BufferedReader(in); |
| 120 | + String line = null; |
| 121 | + while ((line = reader.readLine()) != null) { |
| 122 | + fileContent.add(line); |
| 123 | + } |
| 124 | + reader.close(); |
| 125 | + return fileContent; |
| 126 | + } catch (IOException e) { |
| 127 | + e.printStackTrace(); |
| 128 | + return null; |
| 129 | + } |
| 130 | + } |
74 | 131 | }
|
0 commit comments