Reputation: 176
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());
}
I don't understand how it's not finding the file when providing the full path. The file does exist. It pops on FileReader fr line. Any ideas on how to fix this. Thank you.
Upvotes: 0
Views: 95
Reputation: 573
put your file in your sdcard and try this to read line by line your 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
}
you can also use some library like GSON to convert string to JSON object.
Hope it helps.
Upvotes: 0
Reputation: 2572
I would try using the constructor that takes one "File" argument.
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);
File also has methods that check if the file exists, which are generally very helpful. Maybe the "fileName" one is looking from some base path relative to the project or run-time settings.
Upvotes: 1