<span id="Label3"></p>iOS development-file Management (ii)<p><p><strong>v.. plist File</strong></p></p><p><p><strong>String method to add</strong></p></p><p><p>NSString *path = [nshomedirectory () stringbyappendingpathcomponent:@ "array.plist"];</p></p><p><p>NSString *content = @ "abcd";</p></p><p><p>[contect Writetofile:path Atomically:yes encoding:nsutf8stringencoding error:nil];</p></p><p><p><strong>Array mode add</strong></p></p><p><p>NSString *path = [nshomedirectory () stringbyappendingpathcomponent:@ "array.plist"];</p></p><p><p>[nsarray *array = [[nsarray alloc] initwithobjects:@ "123", @ "798", @ "$", nil]; [array Writetofile:path atomically:yes];</p></p><p><p><strong>Dictionary ways to add</strong></p></p><p><p>NSString *path = [nshomedirectory () stringbyappendingpathcomponent:@ "dic.plist"]; <strong> </strong></p></p><p><p>Nsdictionary *dic = [nsdictionary alloc] initwithobjects:@ "first", @ "second", @ "third" forkeys:@ "123", @ "456", @ "798"]; [dic Writetofile:path atomically:yes];</p></p> <ul> <ul> <li><p><strong>arrays, dictionaries can only write bool, nsnumber, nsstring, nsdata, nsdate, nsarray, nsdictionary to the attribute list plist file</strong></p></li> </ul> </ul><p><p><strong>six, read the file class and common methods</strong></p></p> <ul> <ul> <li><p><strong>The Nsfilehandle class primarily reads and writes the contents of a file</strong></p></li> <li><p><strong>The Nsfilemanager class primarily operates on files (delete, modify, move, copy, and so On)</strong></p></li> </ul> </ul><p><p><strong>Common processing methods</strong></p></p><p><p>+ (id) filehandleforreadingatpath: (nsstring *) Path opens a file ready to <strong></strong> read</p></p><p><p>+ (id) filehandleforwritingatpath: (nsstring *) Path opens a file ready to write</p></p><p><p>+ (id) filehandleforupdatingatpath: (nsstring *) Path opens a file ready for update</p></p><p><p>-(nsdata *) availabledata; Returning available data from a device or channel</p></p><p><p>-(nsdata *) readdatatoendoffile; Reads from the current node to the end of the file</p></p><p><p>-(nsdata *) readdataoflength: (nsuinteger) length; Reads the specified length data starting from the current node</p></p><p><p>-(void) writedata: (nsdata *) data; Write Data</p></p><p><p>-(unsigned Long Long) offsetinfile; Gets the offset of the current file</p></p><p><p>-(void) seektofileoffset: (unsigned Long Long) offset; Jumps to the offset of the specified file</p></p><p><p>-(unsigned Long Long) seektoendoffile; Jump to end of file</p></p><p><p>-(void) truncatefileatoffset: (unsigned Long Long) offset; Set the length of the file to offset bytes</p></p><p><p>-(void) closefile; Close File</p></p><p><p><strong>Append data to a file</strong></p></p><p><p>NSString *homepath = nshomedirectory (); <strong> </strong></p></p><p><p>NSString *sourcepath = [homepath stringbyappendingpathconmpone:@ "testfile.text"];</p></p><p><p>Nsfilehandle *fielhandle = [nsfilehandle filehandleforupdatingatpath:sourcepath];</p></p><p><p>[filehandle seektoendoffile]; To jump a node to the end of a file</p></p><p><p>NSString *str = @ "append data"</p></p><p><p>nsdata* StringData = [str datausingencoding:nsutf8stringencoding];</p></p><p><p>[filehandle writedata:stringdata]; Append Write Data</p></p><p><p>[filehandle closefile];</p></p><p><p><strong>Locating data</strong></p></p><p><p>Nsfilemanager *FM = [nsfilemanager defaultmanager];</p></p><p><p>NSString *content = @ "abcdef";</p></p><p><p>[FM Createfileatpath:path contents:[content datausingencoding:nsutf8stringencoding] attributes:nil];</p></p><p><p>Nsfilehandle *filehandle = [nsfilehandle filehandleforreadingatpath:path];</p></p><p><p>Nsuinteger length = [filehandle availabeldata] length]; Get Data length</p></p><p><p>[filehandle seektofileoffset;length/2]; Half of the offset file</p></p><p><p>NSData *data = [filehandle readdatatoendoffile];</p></p><p><p>[filehandle closefile];</p></p><p><p><strong>Copying files</strong></p></p><p><p>Nsfilehandle *infile, *outfile; Input file, output file</p></p><p><p>NSData *buffer; Buffered data read</p></p><p><p>Nsfilemanager *filemanager = [nsfilemanager defaultmanager];</p></p><p><p>NSString *homepath = nshomedirectory ();</p></p><p><p>NSString *sourcepath = [homepath stringbyappendingpathcomponent:@ "testfile.txt"]; Source file path</p></p><p><p>NSString *outpath = [homepath stringbyappendingpathcomponent:@ "outfile.txt"]; Output file path</p></p><p><p>BOOL sucess = [filemanager Createfileatpath:outpath contents:nil attributes:nil];</p></p><p><p>If (!success)</p></p><p><p>{</p></p><p><p>Return N0;</p></p><p><p>}</p></p><p><p>infile = [nsfilehandle filehandleforreadingatpath:sourcepath]; Create read source Path file</p></p><p><p>if (infile = = Nil)</p></p><p><p>{</p></p><p><p>Return NO;</p></p><p><p>}</p></p><p><p>outfile = [nsfilehandle filehandleforreadingatpath:outpath]; Create a disease open file to output</p></p><p><p>if (outfile = = Nil)</p></p><p><p>{</p></p><p><p>Return NO;</p></p><p><p>}</p></p><p><p>[outfile truncatefileatoffset:0]; Set the length of the output file to 0</p></p><p><p>Buffer = [infile readdatatoendoffile]; Reading data</p></p><p><p>[outfile writedata:buffer]; Write input</p></p><p><p>[infile closefile]; Close write, Input File</p></p><p><p>[outfile closefile];</p></p><p><p></p></p><p><p>iOS development-file Management (ii)</p></p></span>
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