/* 从raw文件夹中读取文件(在res目录下新建的) */public String getFromRaw() { String result = ""; InputStream in = null; try {
in = getResources().openRawResource(R.raw.test1); /* 从raw文件夹中读取文件,不需要后缀 */ int len = in.available(); byte[] msg = new byte[len]; in.read(msg); result = EncodingUtils.getString(msg, ENCODING); } catch (NotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } /* 从assets文件夹中读取文件 */ public String getFromAssets(String filename) { String result = ""; InputStream in = null; try {
in = getResources().getAssets().open(filename); /* 从assets文件夹中获取文件 */ int len = in.available(); byte[] msg = new byte[len]; in.read(msg); result = EncodingUtils.getString(msg, ENCODING); } catch (NotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; }
读取Resources和Assets中的文件
最新推荐文章于 2022-09-25 21:19:04 发布