android java.lang.IllegalArgumentException contains a path separator
16lz
2021-01-25
今天使用android 解析json 文件的时候,一直报个错误:
01-11 05:10:15.727: WARN/System.err(10896):
java.lang.IllegalArgumentException:
File //mnt//sdcard//json.txt contains a path separator:
错误开始前的代码:
try{
FileInputStream fis = this.openFileInput("/mnt/sdcard/json.txt");
//文件中没有数据
if (fis.available() == 0) {
return;
}
byte[] readBytes = new byte[fis.available()];
while (fis.read(readBytes) != -1) {
}
}catch(Exception e){
e.printStackTrace();
}
}
调试后的:
File file = new File("/mnt/sdcard/json.txt");
if (file.exists()) {
FileInputStream fis;
try {
fis = new FileInputStream(file);
fis.available();
byte[] readBytes = new byte[fis.available()];
while (fis.read(readBytes) != -1) {
}
String userData = new String(readBytes);
show.setText(userData);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
注意:
1.FileInputStream 与 openFileInput
2.注意加权限
01-11 05:10:15.727: WARN/System.err(10896):
java.lang.IllegalArgumentException:
File //mnt//sdcard//json.txt contains a path separator:
错误开始前的代码:
try{
FileInputStream fis = this.openFileInput("/mnt/sdcard/json.txt");
//文件中没有数据
if (fis.available() == 0) {
return;
}
byte[] readBytes = new byte[fis.available()];
while (fis.read(readBytes) != -1) {
}
}catch(Exception e){
e.printStackTrace();
}
}
调试后的:
File file = new File("/mnt/sdcard/json.txt");
if (file.exists()) {
FileInputStream fis;
try {
fis = new FileInputStream(file);
fis.available();
byte[] readBytes = new byte[fis.available()];
while (fis.read(readBytes) != -1) {
}
String userData = new String(readBytes);
show.setText(userData);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
注意:
1.FileInputStream 与 openFileInput
2.注意加权限
更多相关文章
- 一款常用的 Squid 日志分析工具
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
- Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
- android选择图片或拍照图片上传到服务器(包括上传参数)
- Android(安卓)App程序结构
- Android常见的几种RuntimeException
- android6.0编译失败
- Android初学者—listView用法