Recently, we need to collect some data logs in the simulator environment.
The data recording and appending functions are also taken into account.
The simple code is as follows:
+ (Void) appstring :( nsstring *) s {bool isexit = [nlcommonutil isexitoffile: @ "nonotextiphone"]; nsstring * filepath = [self filepathindocumentbyfilename: @ "nonotextiphone"]; if (! Isexit) {nslog (@ "% @", @ "file does not exist"); nsstring * s = [nsstring stringwithformat: @ "started: \ r"]; [s writetofile: filepath atomically: Yes encoding: nsutf8stringencoding error: Nil];} nsfilehandle * OUTFILE; nsdata * buffer; OUTFILE = [nsfilehandle filehandleforwritingatpath: filepath]; If (OUTFILE = nil) {nslog (@ "open of file for writing failed");} // locate and locate the end position of OUTFILE (append the file here) [OUTFILE seektoendoffile]; // read infile and write its content to OUTFILE nsstring * BS = [nsstring stringwithformat: @ "% @", S]; buffer = [BS datausingencoding: nsutf8stringencoding]; [OUTFILE writedata: Buffer]; // close the read/write file [OUTFILE closefile];}
The code is relatively simple. Because of the value of a text file, the file name is directly written to death.
You can add a file name parameter to the method.
1. Check whether the file exists. If the file does not exist, create it.
2. Use the filehandle class to append text.
It's easy to add an Android app, because this app also needs to be tested on Android.
Public static void appstring (string s) {string Path = environment. getexternalstoragedirectory () + "/download_jfk/nonotextandroid"; filewriter writer; file F = new file (PATH); If (! F. exists () {try {f. createnewfile (); writer = new filewriter (f); writer. write ("started: \ r"); writer. close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}try {writer = new filewriter (path, true); writer. write (s); writer. close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}
~~~~~~~