File read/write

Source: Internet
Author: User

A. String, dictionary, array store to local file

strings, arrays, and dictionaries are stored in the same way as local files, except that the data types to be stored are different, where strings are stored in local files, for example, as follows

nsstring*content [email protected] "save string to local file"

;

(1) Obtaining documents

Folder path

Parameters: (1) Specify the folder, (2) Set the lookup domain, (3) whether to use the detailed path

Nsstring*documentspath = [Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes) Firstobject];

(2) Stitching on the file path to be stored (the front automatically plus/), if no this file system will automatically create a

Nsstring*newfielpath = [documentspathstringbyappendingpathcomponent:@ "Aa.txt"];

(3) Store content to the specified file path

Nserror*error =nil;

The string is written to the local file parameter: (1) The file path to store the content, (2) whether to use atomic attributes, (3) storage format

Boolissucceed = [Contentwritetofile:newfielpath atomically:YESencoding:NSUTF8StringEncoding error:&error];

Two. String, dictionary, array, read from local file

strings, dictionaries, and arrays are read from a local file, just like the data types to be read, here is an example of reading a string from a local file, as follows

1.

Get the path to the read file

(1) Get Documents folder path

Nsstring*documentspath = [Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes) Firstobject];

(2) Stitching on The file path to be stored (the front automatically plus/), if no this file system will automatically create a

Nsstring*newfielpath = [documentspathstringbyappendingpathcomponent:@ "Aa.txt"];

(3)

Reading data

Nserror*error =nil; (&error can last)

NSString *content = [NSString stringwithcontentsoffile:newfielpath encoding:nsutf8stringencoding Error:nil]

Three. Summary

File reads and writes are only supported temporarily, Nsstring,nsarray,nsdictionary,nsdata and their subclasses

Write file Method: Writetofiel: Object Call method

Read files: Each class comes with a way to read files according to the file path: [Class name + Withcontentoffiel], as follows

NSString: [NSString Stringwithcontentoffile:]

Nsarry

: [Nsarry Arraywithcontentoffiel:]

Nsdictionary

: [Nsdictionary Dictionarywithofcontentfile:]

Binary stream: [NSData Datawithofcontentfiel:] For nsarray,nsdictionary, the inner member must also be one of the eight classes that can implement file read and write

File read/write

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.