开发者

Problem finding/reading a file

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.


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();
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜