Append Text File Content in IOS

Source: Internet
Author: User

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 ();}}

~~~~~~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.