Writing to an External File on Android --- File Doesn't Register, But Java Can Read -


i'm trying write external txt (or csv) file android. can run app, close it, , run again, , readdata() read log i've stored. however, dirfile (file directory) appears within android files (even if connect computer , search).

something interesting, though: if clear log (similar list of print statements shown within eclipse) , disconnect phone computer, reconnect it, log reappears i've ever written file (even if later overwrote it)...yet app isn't running!

here code. please me understand why cannot find file!

(note: i've tried appending "myfile.txt" extension directory, causes eisdir exception.)

public void writedata(string dirname){      try     {         file root = new file(getexternalfilesdir(null), dirname);          // writes file         //         // "true" argument allows file appended. without argument (just root),         // file overwritten (even though later call append) rather appended to.          filewriter writer = new filewriter(root, true);         writer.append("append text\n");         writer.flush();         writer.close();          // checks if wrote file reading in (appears in log)          //readdata(dirname);     }     catch(exception e)     {          log.v("2222", "2222 error: " + e.getmessage());     } } 

if you're interested, here's function wrote read in data:

public void readdata(string dirname){      try     {         file root = new file(getexternalfilesdir(null), dirname);          // checks see if writing file reading in file          bufferedreader reader = new bufferedreader(new filereader(root));         try {             string s = reader.readline();             while (s != null) {                 log.v("2222", "2222 read: " + s);                 s = reader.readline();             }         }         catch(exception e) {              log.v("2222", "2222 error: " + e.getmessage());         }         {             reader.close();         }     }     catch(exception e) {          log.v("2222", "2222 error: " + e.getmessage());     } } 

thanks!

even if connect computer , search

if clear log (similar list of print statements shown within eclipse) , disconnect phone computer, reconnect it, log reappears i've ever written file (even if later overwrote it).

what seeing on computer indexed mediastore, , possibly subset of those, depending upon whether computer caches information gets device in terms of "directory" contents.

to ensure mediastore indexes file promptly:

  1. use fileoutputstream (optionally wrapped in outputstreamwriter), not filewriter

  2. call flush(), getfd().sync(), , close() on fileoutputstream, instead of calling flush() , close() on filewriter (sync() ensure bytes written disk before continuing)

  3. use mediascannerconnection , scanfile() tell mediastore index file

you can use whatever sort of "reload" or "refresh" or whatever option in desktop os's file manager, , file should show up.

this blog post has more on of this.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -