string - Android Studio Saving Data to Phone -
string mystring = "hello world";
how save contents of mystring
.txt file within phone/android, there after application ends , can edit value in future?
use method:
public void writetofile(string str) try { outputstreamwriter outputstreamwriter = new outputstreamwriter(context.openfileoutput("config.txt", context.mode_private)); outputstreamwriter.write(data); outputstreamwriter.close(); } catch (ioexception e) { log.e("exception", "file write failed: " + e.tostring()); } private string readfromfile() { string ret = ""; try { inputstream inputstream = openfileinput("config.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; }
Comments
Post a Comment