android - Read content of JSON File from Internal Storage -
every bigger in , wanted know how can output content of json file internal storage. following working it.
string filename = "names.json"; final file file = new file(environment.getdatadirectory(), filename); log.d(tag, string.valueof(file));
the log shows as: /data/names.json
names.json
[ "names", { "name": "john doe" } ]
read string file , convert jsonobject
or jsonarray
string jsongstring = readfromfile(); jsonarray jarray = new jsonarray(str);
use below method read data internal storage file , return string
.
private string readfromfile() { string ret = ""; try { inputstream inputstream = openfileinput("names.json"); if ( inputstream != null ) { inputstreamreader inputstreamreader = new inputstreamreader(inputstream); bufferedreader bufferedreader = new bufferedreader(inputstreamreader); string receivestring = ""; stringbuilder stringbuilder = new stringbuilder(); while ( (receivestring = bufferedreader.readline()) != null ) { stringbuilder.append(receivestring); } inputstream.close(); ret = stringbuilder.tostring(); } } catch (filenotfoundexception e) { log.e("login activity", "file not found: " + e.tostring()); } catch (ioexception e) { log.e("login activity", "can not read file: " + e.tostring()); } return ret; }
Comments
Post a Comment