I'm trying to develop a small application, where I was given a JSON file, and I have to extract data from it. As I understood a JSON object takes a string argument, thus I'm trying to access a file and write the data from it to a string. I've placed that file in a "JSON file" folder, and when I try to read the file, it throws me a file not found exception. I've tried several ways to find a path to that file, but every attempt was for vain. It might be that I'm extracting the path wrong, or might be something else, please help me. Thanks in advance. here is the code of finding the path:
try
{
path = Environment.getRootDirectory().getCanonicalPath();
}
catch (IOException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
File jFile = new File(path + /"JSON file/gallery.json");
here is the code for reading from a file :
String str ="";
try
{
BufferedReader in = new BufferedReader(new FileReader(jFile));
while ((str += in.readLine()) != null)
{
}
in.close();
}
catch (IOException e)
{
e.getMessage();
}
return str;
Here more specification: I want to take the data from the file in order to do that : JSONObject(jString). when I extract the path of json file I create a file with the path and pass it to the function that reads from the file, and there it throws me a file not found exception, when I try to read from it. The file does exists in the folder (even visually - I've tried to attach an image but the site won't let me, because I'm new user) I've tried to open the file through the windows address bar by typing the path like that: C:\Users\Marat\IdeaProjects\MyTask\JSON file\gallery.json and it opens it.