android 读取资源文件的数据
1,读取assets文件夹中的数据:
try {
InputStream is = getResources().getAssets().open("info.txt");
InputStreamReader reader = new InputStreamReader(is, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(reader);
String line = "";
while ((line=bufferedReader.readLine())!=null)
Log.d("===", "readLine:"+line);
} catch (IOException e) {
e.printStackTrace();
}
2,读取raw文件夹中的数据:
InputStream is = getResources().openRawResource(R.raw.info);
InputStreamReader reader = null;
try {
reader = new InputStreamReader(is, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(reader);
String line = "";
while ((line=bufferedReader.readLine())!=null){
Log.d("===", "readLine:"+line);
}
}catch (IOException e) {
e.printStackTrace();
}
3,写内部存储的文件数据(文件保存在data/data/包名/file 下)
try {
FileOutputStream fos = openFileOutput(fileName, MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
osw.write(数据);
osw.flush();
fos.flush();
osw.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
4,读内部存储的文件数据(文件路径:data/data/包名/file 下)
try {
FileInputStream fis = openFileInput(fileName);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
char[] input = new char[fis.available()];
isr.read(input);
isr.close();
fis.close();
String result = new String(input);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
更多相关文章
- mybatisplus的坑 insert标签insert into select无参数问题的解决
- python起点网月票榜字体反爬案例
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- 读取android手机流量信息
- android 使用html5作布局文件: webview跟javascript交互
- 《Android开发从零开始》——25.数据存储(4)
- Android(安卓)多媒体扫描过程(Android(安卓)Media Scanner Proces
- Android系统配置数据库注释(settings.db)