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