0

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.

Marat
  • 35
  • 5

2 Answers2

0

EDIT

You need to put the file in the device, if it is on your computer, it is not accessible from your device. There are some ways to do that, and one of them is to put it in the res/ dir of your application. Please refer to the documentation to see how to do that.


Debug it. I'm pretty sure it will be very easy to find. To start with, look for the following:

  1. Print the path before you create the file, e.g. Log.d("SomeTag", path + "/JSON file/gallery.json")

  2. Observe the full exception details. Maybe there is another problem.

  3. Explore the folders and see if the file exists (in eclipse: window -> show view -> other -> android -> file explorer.

You will probably observe the problem and be able to fix it. If not, post here a question with more details, including the results of those trials.

BTW, GetgetRootDirectory() returns the root directory of android, that's not what you want (you don't have RW permissions there) you probably want to get the applcation directory, you can see how to get it here, in the question I asked a few month ago. But since you didn't give us those details, it will be hard to help you more then that.

Community
  • 1
  • 1
MByD
  • 135,866
  • 28
  • 264
  • 277
  • Ok, the result of a log: /system/JSON file/gallery.json – Marat Jul 20 '11 at 21:54
  • Please, before we are dragged into endless comment thread, try to look for all mentioned stuff, then edit your question in a way that will help us. – MByD Jul 20 '11 at 21:59
  • edited, I hope I mentioned all. BTW the the exception is: java.io.FileNotFoundException: /system/JSON file/gallery.json – Marat Jul 20 '11 at 22:23
0

if you store it in the assets folder you can access it by using

InputStream is = context.getResources().getAssets().open("sample.json");

You can then convert it to a String

public static String inputStreamAsString(InputStream stream)
    throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = br.readLine()) != null) {
        sb.append(line + "\n");
    }
    br.close();
    return sb.toString();
}
Kevin Qiu
  • 1,616
  • 1
  • 13
  • 15
  • How do I get the string from it? – Marat Jul 20 '11 at 23:30
  • I added a method to convert the inputstream to a string – Kevin Qiu Jul 20 '11 at 23:34
  • I'm sorry about the silly questions (I'm using android for about 10 hours, I'm very much a rookie), but because you have used an instance, I had to add Context context, but it has to be initialized, how do I initialize it? – Marat Jul 20 '11 at 23:46
  • um if youre doing it inside an activity you don't need context. technically context would be this – Kevin Qiu Jul 20 '11 at 23:47
  • Thank you very much, that solved it, and tell me please, if I need to store any data, the best place is in the assets folder, or I can store it anywhere? – Marat Jul 20 '11 at 23:54
  • well the assets folder is only for files that you bundle with the app. if you download a file in the app you will have to put it elsewhere. Check this. http://developer.android.com/guide/topics/data/data-storage.html#filesInternal – Kevin Qiu Jul 21 '11 at 00:11