Android讀取asserts和raw檔案夾下的檔案

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   color   os   ar   使用   

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檔案夾下的檔案資源ID13      * @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

Android讀取asserts和raw檔案夾下的檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.