android java.lang.IllegalArgumentException contains a path separator

今天使用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.注意加權限
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章