input - Android: Read String from file -
i having trouble reading string ".txt" file on external storage. method created not working program , need it
private string readfromfile() { string ret = ""; try { final file configdir = new file(environment.getexternalstoragedirectory(), "config"); configdir.mkdir(); inputstream inputstream = openfileinput(new file(configdir, "url.txt")); 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; }
i not know how solve , method work program. great if of me.
use filereader
instead of inputstreamreader
, correct configdir
creation below...
try { final file configdir = new file(environment.getexternalstoragedirectory() + "/config"); configdir.mkdir(); file file = new file(configdir, "url.txt"); if ( inputstream != null ) { bufferedreader bufferedreader = new bufferedreader(new filereader(file)); 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()); }
Comments
Post a Comment