android - Problem finding/reading a file -


i'm trying develop small application, given json file, , have extract data it. understood json object takes string argument, i'm trying access file , write data string. i've placed file in "json file" folder, , when try read file, throws me file not found exception. i've tried several ways find path file, every attempt vain. might i'm extracting path wrong, or might else, please me. in advance. here code of finding 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 code reading 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: want take data file in order : jsonobject(jstring). when extract path of json file create file path , pass function reads file, , there throws me file not found exception, when try read it. file exists in folder (even visually - i've tried attach image site won't let me, because i'm new user) i've tried open file through windows address bar typing path that: c:\users\marat\ideaprojects\mytask\json file\gallery.json , opens it.

if store in assets folder can access using

inputstream = context.getresources().getassets().open("sample.json"); 

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

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -