【Adnroid】读取raw的txt等文本文件
有时候我我们想要把文本内容储存在一个txt文件里,需要的时候再读取
1.先在项目的res文件夹下新建一个raw文件夹,把 文本文件复制到raw文件夹
2.读取raw文件的方法
public static String readFileFromRaw(Activity context, int rawName) {BufferedReader reader = null;try {InputStreamReader inputReader = new InputStreamReader(context.getResources().openRawResource(rawName));reader = new BufferedReader(inputReader);String line = "";String result = "";while ((line = reader.readLine()) != null)result += line;return result;} catch (Exception e) {e.printStackTrace();}finally {if (reader !=null){try {reader.close();}catch (Exception e){e.printStackTrace();}}}return "";}
3.调用方法
String s = readFileFromRaw(MainActivity.this,R.raw.text);
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
