try {
FileReader fr = new FileReader("C:\\Users\\kevin\\Desktop\\AndroidLibr\\LeagueStats\\app\\src\\main\\java\\com\\example\\laura\\myapplication\\champions.json");
BufferedReader br = new BufferedReader(fr);
ChampionData championData = gson.fromJson(br, ChampionData.class);
} catch (FileNotFoundException e) {
Log.i("exception", e.getMessage());
}
我不明白提供完整路径时怎么找不到文件。该文件确实存在。它在FileReader fr行上弹出。有想法该怎么解决这个吗。谢谢。
答案 0 :(得分:1)
我会尝试使用带有一个“ File”参数的构造函数。
File file = new File("C:\\Users\\kevin\\Desktop\\AndroidLibr\\LeagueStats\\app\\src\\main\\java\\com\\example\\laura\\myapplication\\champions.json");
FileReader fr = new FileReader(file);
文件还具有检查文件是否存在的方法,通常非常有用。也许“ fileName”是从某个相对于项目或运行时设置的基本路径中查找的。 p>
答案 1 :(得分:0)
将文件放入sdcard中,并尝试逐行读取json:
//Find the directory for the SD Card
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"champions.json");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
//read line by line
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}
您还可以使用GSON之类的库将字符串转换为JSON对象。
希望有帮助。