文章目录

工具类,用于读取资源目录下的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
public class ResourceUtils {
// 读取assets目录下某个文件内容
public static String getFileFromAssets(Context context,String fileName){
if(context == null || TextUils.isEmpty(fileName)) {
return null;
}
StringBuilder s = new StringBuilder("");
try {
InputStreamReader in = new InputStreamReader(context.getResources().getAssets().open(fileName));
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null){
s.append(line);
}
return s.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
// 读取raw目录下某个文件内容
public static String getFileFromRaw(Context context,int resId) {
if (context == null) {
return null;
}

StringBuilder s = new StringBuilder();
try {
InputStreamRead in = new InputStreamReader(context.getResources().openRawResource(resId));
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine() != null)){
s.append(line);
}
return s.toString();
} catch (IOException e) {
e.printStatckTrace();
return null;
}
}

public static List<String> getFileToListFromAssets(Context context,String fileName) {
if (context == null || TextUtils.isEmpty(fileName)) {
return null;
}
List<String> fileContent = new ArrayList<String>();
try {
InputStreamReader in = new InputStreamReader(context.getResouces().getAssets().open(fileName));
BufferedReader br = new BufferedReader(in);
String line;
while((line = br.readLine() != null)) {
fileContent.add(line);
}
br.close();
return fileContent;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

public static List<String> getFileToListFromRaw(Context context,int resId) {
if (context == null) {
return null;
}
List<String> fileContent = new ArrayList<String> ();
BufferedReader reader = null;
try {
InputStreamReader in = new InputStreamReader(context.getResouces().openRawResource(resId));
reader = new BufferedReader(in);
String line = null;
while((line = reader.readLine()) != null){
fileContent.add(line);
}
reader.close();
return fileContent;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}

文章目录