Skip to content

Instantly share code, notes, and snippets.

@SeniorZhai
Last active August 29, 2015 14:05
Show Gist options
  • Save SeniorZhai/a2858ab65707b94fb00e to your computer and use it in GitHub Desktop.
Save SeniorZhai/a2858ab65707b94fb00e to your computer and use it in GitHub Desktop.
Resource管理工具
public class ResourceUtils {
public static String getFileFromAssets(Context context,String fileName) {
if(context == null || TextUtils.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;
}
}
public static String geFileFromRaw(Context context, int resId) {
if (context == null) {
return null;
}
StringBuilder s = new StringBuilder();
try {
InputStreamReader 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.printStackTrace();
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.getResources().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> geFileToListFromRaw(Context context, int resId) {
if (context == null) {
return null;
}
List<String> fileContent = new ArrayList<String>();
BufferedReader reader = null;
try {
InputStreamReader in = new InputStreamReader(context.getResources().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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment