java - Android internal files seems to disappear after I restart my application -
i new android , trying save few files in little "play-around" application making learn, files seem persist until application shut down. when run:
file.exists() file.isfile()
or similar methods return false when application restarted.
this method found online save files:
public void writetofile(string data, string filepath, context context){ file file = new file(context.getfilesdir(), filepath); if(!file.exists()){ try{ file.createnewfile(); }catch(ioexception e) { e.printstacktrace(); log.e("exception", "file couldn't created"); } } try { outputstreamwriter outputstreamwriter = new outputstreamwriter(context.openfileoutput(filepath, context.mode_private)); outputstreamwriter.write(data); outputstreamwriter.close(); } catch (ioexception e) { log.e("exception", "file write failed: " + e.tostring()); } }
i have been googling lot trying find solution none of them worked. seems work when application running since can write , read files, @ restart of application, cannot find files. (file.exists() returns false)
i assume attempting write internal storage here? if not , wish access external storage, suggest having through guidance on android.com below.
https://developer.android.com/guide/topics/data/data-storage.html#filesinternal
if is internal storage trying write to, need make sure passing correct context method. example, if running mainactivity class , method contained within class, call along lines of:
writetofile("some data", "afile.txt", mainactivity.this)
Comments
Post a Comment