Skip to content

Commit 2430f0a

Browse files
committed
Add read raw or assets file to list
1 parent 7917d5c commit 2430f0a

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

src/cn/trinea/android/common/util/ResourceUtils.java

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import java.io.BufferedReader;
44
import java.io.IOException;
55
import java.io.InputStreamReader;
6-
7-
import cn.trinea.android.common.util.StringUtils;
6+
import java.util.ArrayList;
7+
import java.util.List;
88

99
import android.content.Context;
1010

@@ -71,4 +71,61 @@ public static String geFileFromRaw(Context context, int resId) {
7171
return null;
7272
}
7373
}
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+
}
74131
}

0 commit comments

Comments
 (0)