Android读取asserts和raw文件夹下的文件

Android读取asserts和raw文件夹下的文件

经常需要用到读取“/res/raw”和"/asserts"文件夹下的文件,索性写成工具类方便以后使用。


一、raw文件夹下的文件操作工具类:

 1 /**
 2  * raw文件夹下的文件处理工具类
 3  * 
 4  * */
 5 public class RawFileUtils {
 6     private RawFileUtils( ){
 7         
 8     }
 9     
10     /**
11      * 读取raw文件夹下的文件
12      * @param resourceId raw文件夹下的文件资源ID
13      * @return 文件内容
14      * 
15      * */
16     public static String readFileFromRaw(Context context, int resourceId) {
17         if( null == context || resourceId < 0 ){
18             return null;
19         }
20         
21         String result = null;
22         try {
23             InputStream inputStream = context.getResources().openRawResource( resourceId );
24             // 获取文件的字节数
25             int length = inputStream.available();
26             // 创建byte数组
27             byte[] buffer = new byte[length];
28             // 将文件中的数据读到byte数组中
29             inputStream.read(buffer);
30             result = EncodingUtils.getString(buffer, "utf-8");
31         } catch (Exception e) {
32             e.printStackTrace();
33         }
34 
35         return result;
36     }
37 }

二、asserts文件夹下的文件操作工具类:

 1 /**
 2  * asserts文件处理
 3  * 
 4  * */
 5 public class AssertsFileUtils {
 6     private AssertsFileUtils( ){
 7         
 8     }
 9     
10     /**
11      * 读取asserts目录下的文件
12      * @param fileName eg:"updatelog.txt"
13      * @return 对应文件的内容
14      * 
15      * */
16     public static String readFileFromAssets(Context context, String fileName) throws IOException, IllegalArgumentException {
17         if (null == context || TextUtils.isEmpty( fileName )){
18             throw new IllegalArgumentException( "bad arguments!" );
19         }
20         
21         AssetManager assetManager = context.getAssets();
22         InputStream input = assetManager.open(fileName);
23         ByteArrayOutputStream output = new ByteArrayOutputStream();
24         byte[] buffer = new byte[1024];
25         int length = 0;
26         while ((length = input.read(buffer)) != -1) {
27             output.write(buffer, 0, length);
28         }
29         output.close();
30         input.close();
31         
32         return output.toString();
33     }
34     
35     /**
36      * 列出Asserts文件夹下的所有文件
37      * @return asserts目录下的文件名列表
38      * 
39      * */
40     public static List<String> getAssertsFiles( Context context ) throws IllegalArgumentException{
41         if( null == context ){
42             throw new IllegalArgumentException( "bad arguments!" );
43         }
44         
45         AssetManager assetManager = context.getAssets();
46         String[] files = null;
47         try {
48             files = assetManager.list("");
49         } catch (IOException e) {
50             e.printStackTrace( );
51         }
52         
53         return ( null == files )?null:Arrays.asList( files );
54     }
55 }

三、实例

 1 public class MyActivity extends Activity{
 2     
 3     public static final String ENCODING = "UTF-8";
 4     TextView tv1;
 5     TextView tv2;
 6     
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.main);
11         tv1 = (TextView)findViewById(R.id.tv1);
12         tv1.setTextColor(Color.RED);
13         tv1.setTextSize(15.0f);
14         tv2 = (TextView)findViewById(R.id.tv2);
15         tv2.setTextColor(Color.RED);
16         tv2.setTextSize(15.0f);
17         tv1.setText(getFromRaw());
18         tv2.setText(getFromAssets("test2.txt"));
19     }
20     
21     //从resources中的raw 文件夹中获取文件并读取数据
22     public String getFromRaw(){
23         String result = "";
24             try {
25                 InputStream in = getResources().openRawResource(R.raw.test1);
26                 //获取文件的字节数
27                 int lenght = in.available();
28                 //创建byte数组
29                 byte[]  buffer = new byte[lenght];
30                 //将文件中的数据读到byte数组中
31                 in.read(buffer);
32                 result = EncodingUtils.getString(buffer, ENCODING);
33             } catch (Exception e) {
34                 e.printStackTrace();
35             }
36             return result;
37     }
38     
39     //从assets 文件夹中获取文件并读取数据
40     public String getFromAssets(String fileName){
41         String result = "";
42             try {
43                 InputStream in = getResources().getAssets().open(fileName);
44                 //获取文件的字节数
45                 int lenght = in.available();
46                 //创建byte数组
47                 byte[]  buffer = new byte[lenght];
48                 //将文件中的数据读到byte数组中
49                 in.read(buffer);
50                 result = EncodingUtils.getString(buffer, ENCODING);
51             } catch (Exception e) {
52                 e.printStackTrace();
53             }
54             return result;
55     }
56 }

参考:http://blog.csdn.net/ekeuy/article/details/39479201
原文地址:https://www.cnblogs.com/larack/p/4058902.html