java - Continuously writing to a text file in Android -


i understand question has been asked many times before, cannot find example caters towards needs. have asked questions on stackoverflow few times before, , there gents ask me "go read book" or "do research" before asking here. well, have done that, , didn't find enough information - if you're 1 of people, appreciate if point me resource know dig.

anyway, finished writing application calculates bandwidth, link speed, , other attributes of device. bandwidth gets refreshed every 5 seconds, , link speed , data connection refreshes every second. currently, information displayed on screen.

i'm trying create greater-schemed project involves computer (a command center) monitoring connectivity of device, , this, device has save these values text file can ssh'd command center. question trivial; how go writing data text file? know how in java; use fileoutputstream, or combine filewriter + printwriter achieve similar effect. however, don't know how directories work in android, , i'm not sure whether there precautions should aware of while writing files in android. notice need state like:

<uses-permission android:name="android.permission.write_external_storage"/> 

in manifest. there else should doing? also, needs, can recommend way write output effectively? fileoutputstream way go, seems easy? should tokenize data specific way, such splitting tab? i'm new programming , i'm still learning, appreciate or recommendations of useful readings.

this website, presumably go-to reading, down. if provide me snippets of examples, appreciated.

android directories work similar pc directories, exception being can have different 'root' directories, such internal or external storage. make little more confusing, phones have internal sd card - writing sd card may not external sd card. you're going want flexible when write text file - if user doesn't have sd card (external storage) need change game plan. see this post more info detecting sd card.

as actual writing of text file, it's pretty trivial:

edit: should use getexternalfilesdir(null) absolute external path app. must inside of activity call (or have access context object).

   file file = new file(getexternalfilesdir(null), "stats.txt");    try    {       bufferedwriter buf = new bufferedwriter(new filewriter(file));        buf.append(text);       buf.newline();       buf.close();    }    catch (ioexception e)    {       // todo auto-generated catch block       e.printstacktrace();    } 

note there dozens of ways file i/o in android, , 1 of them. here blog post detailing issue.

to store data, 1 of 2 things:

first, store data csv (comma-seperated value) string, this:

12,345,6,789 

then when parse know first number bandwidth, second speed, etc. rather use comma delimiter tab because i'm not huge fan of using whitespaces delimiter.

secondly, use key/value pair setup, this:

bandwidth=12 speed=34 

then in parsing code, can split on = character. first string key , second value.


Comments