Skip to content

Commit

Permalink
Add read raw or assets file to list
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinea committed Jul 10, 2014
1 parent 7917d5c commit 2430f0a
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions src/cn/trinea/android/common/util/ResourceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import cn.trinea.android.common.util.StringUtils;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

Expand Down Expand Up @@ -71,4 +71,61 @@ public static String geFileFromRaw(Context context, int resId) {
return null;
}
}

/**
* same to {@link ResourceUtils#geFileFromAssets(Context, String)}, but return type is List<String>
*
* @param context
* @param fileName
* @return
*/
public static List<String> geFileToListFromAssets(Context context, String fileName) {
if (context == null || StringUtils.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;
}
}

/**
* same to {@link ResourceUtils#geFileFromRaw(Context, int)}, but return type is List<String>
*
* @param context
* @param resId
* @return
*/
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;
}
}
}

0 comments on commit 2430f0a

Please sign in to comment.