IOS growth path-NSData and NSFileManager examples

Source: Internet
Author: User
// Nsdata complies with the nscopying nscoding protocol. It provides object-oriented array storage for byte // applicable and read/write files, while a buffer zone is required for file read/write, nsdate provides such a cache zone // defines a char-type string const char * string = "Hi there, this is a C string"; // creates a buffer zone, add the string to nsdata * Data = [nsdata datawithbytes: String Length: strlen (string) + 1]; // output nslog (@ "data is % @", data ); nslog (@ "% lu bytes string is '% S'", [Data Length], [data bytes]); // defines a string, save a path nsstring * Path = @ "/tmp/ver.txt"; // Save the string of the saved path to another file. encoding is encoded [path writetofile: @ "/tmp/string.txt" atomically: Yes encoding: nsasciistringencoding error: Nil]; // Add an array and add several strings nsarray * phrase; phrase = [nsarray arraywithobjects: @ "I", @ "good", @ "seem", @ "to", nil]; // write the Array (the string path defined above) [Phrase writetofile: path atomically: Yes]; // print nslog (@ "% @", phrase ); // create a file manager/* nsfilemanager can be used to query single dictionary directories, create, rename, delete directories, and obtain/set file attributes */nsfilemanager * FM; fm = [nsfilemanager defaultmanager]; // create a buffer and use the nsfilemanager object to obtain the content in the file. That is, you can modify the nsdata * filedata; filedata = [FM contentsatpath: @ "/tmp/ver.txt"]; // print nslog (@ "file data is % @", filedata); // judge the nsdata object if (filedata) {nslog (@ "File Read success");} else {nslog (@ "File Read failed") ;}// defines a Boolean object bool ifsucess; // In objective-C, the correct is yes. The error is no ifsucess = no; // obtain the content of the file obtained from the above filedata object through the nsfilemanager object, create a new path and store ifsucess = [FM createfileatpath: @ "/tmp/test4.txt" Contents: filedata attributes: Nil]; // judge the Boolean object if (ifsucess) {nslog (@ "Create File sucess");} else {nslog (@ "Create File failed ");}

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.