0
点赞
收藏
分享

微信扫一扫

安卓 读取raw assets文件

他说Python 2022-06-20 阅读 160
/**
* 从assets中读取txt
*/
private void readFromAssets() {
try {
InputStream is = MyApplication.getContext().getAssets().open("qq.txt");
String text = readTextFromSDcard(is);
// textView.setText(text);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 从raw中读取txt
*/
private void readFromRaw() {
try {
InputStream is = MyApplication.getContext().getResources().openRawResource(R.raw.qq);
String text = readTextFromSDcard(is);
XLog.showArgsInfo("text = " + text);
// textView.setText(text);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 按行读取txt
*
* @param is
* @return
* @throws Exception
*/
private String readTextFromSDcard(InputStream is) throws Exception {
InputStreamReader reader = new InputStreamReader(is);
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuffer buffer = new StringBuffer("");
String str;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
buffer.append("\n");
}
return buffer.toString();
}

忘在哪儿看到的了,侵删


举报

相关推荐

0 条评论