android 如何读取 assets、raws文件

 时间:2026-02-16 11:42:45

1、读输入流的方法:通过读输入流返回一个String

/**

 * 读输入流

 * 

 * @param is

 * @return

 */

public static String getStringByInputStream(InputStream is) {

String content = null;

try {

if (is != null) {

byte[] buffer = new byte[is.available()];

ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();

while (true) {

int readLength = is.read(buffer);

if (readLength == -1)

break;

arrayOutputStream.write(buffer, 0, readLength);

}

is.close();

arrayOutputStream.close();

content = new String(arrayOutputStream.toByteArray());

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

content = null;

} finally {

try {

if (is != null)

is.close();

} catch (IOException ioe) {

ioe.printStackTrace();

}

}

return content;

}

2、读assets文件的方法:

public static String readAssets(Context context, String fileName) {

InputStream is = null;

try {

is = context.getAssets().open(fileName);

} catch (IOException e) {

e.printStackTrace();

}

return getStringByInputStream(is);

}

3、读raw文件的方法:

public static String readRaws(Context context, int id) {

InputStream is = context.getResources().openRawResource(id);

return getStringByInputStream(is);

}

  • 丹参应该怎样种植?
  • 使用JS获取页面URL中的锚点值实现特定跳转
  • 怎么洗去玻璃窗上的水泥点
  • Axure如何制作短信验证倒计时效果
  • img对象如何使用alt属性?
  • 热门搜索
    关于五一劳动节手抄报 安全手抄报内容资料 手抄报图片花边 手抄报端午节 2020国庆节手抄报内容 英语手抄报资料 英语手抄报设计 我爱读书手抄报图片 关于文学的手抄报 奥运手抄报