Dark Horse programmer--foundation--nsfilemanager and Nsfilehandle

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Nsfilemanager
#import <Foundation/Foundation.h> #define PATH @ "/users/fengze/desktop" int main (int argc, const char * argv[]) {@ Autoreleasepool {//Create File Manager Object Nsfilemanager *fm = [Nsfilemanager Defaultmanager]; View the specified file path and return an array of directories storing files and folders Nsarray *array = [FM contentsofdirectoryatpath:path Error:nil]; The above method is only a shallow traversal, can not traverse the contents of the folder//depth traversal array = [FM Subpathsofdirectoryatpath:path E Rror:nil]; You can iterate through all the contents of the current folder//create directory [FM createdirectoryatpath:[nsstring stringwithformat:@ "%@/Exercise", PATH ] Withintermediatedirectories:no Attributes:nil Error:nil]; The above meaning is to create a dir file in the PATH directory//create a file [FM createfileatpath:[nsstring stringwithformat:@ "% @dir", PATH] Conte nts:[@ "Hello" datausingencoding:nsutf8stringencoding] attributes:nil]; A hello file was created in the Dir folder, but what is the extension list file created? //deletion of files//[FM removeitematpath:[nsstring stringwithformat:@]%@/dIR ", PATH] error:nil]; Will delete all contents of the directory//file copy [FM copyitematpath:[nsstring stringwithformat:@ "%@/dir", PATH] Topath:[nsstrin G stringwithformat:@ "%@/dir2", PATH] error:nil]; File movement [FM moveitematpath:[nsstring stringwithformat:@ "%@/dir", PATH] topath:[nsstring stringwithformat:@ "% @/dir2 ", PATH] error:nil]; /* File deletion, file copy, file movement can be used both on the file, or can be used on the folder */} return 0;

Nshandle

#import <foundation/foundation.h>//nshandle called file handle int main (int argc, const char * argv[]) {@autoreleasepool { Open file generation file handle in read-only mode nsfilehandle *FH = [Nsfilehandle filehandleforreadingatpath:@ "/users/fengze/desktop                /test/"]; /* files from hard disk to memory called read, from memory to hard disk called write, memory equivalent to brain, hard disk equivalent to textbook */////Read file contents according to specified range nsdata *data = [FH Read                Dataoflength:3];    data = [FH Readdataoflength:5]; The second read will then continue reading the last read//Read all the contents of the file one time nsstring *str = [[NSString alloc] Initwithdata:data encoding                : Nsutf8stringencoding];                        Open file generation file handle in write-only mode nsfilehandle *FH2 = [Nsfilehandle filehandleforwritingatpath:@ "/users/fengze/desktop/test"];        [Fh2 writedata:[@ "Hello" datausingencoding:nsutf8stringencoding]];        The content written will replace the previous contents of the file, and the contents of the following are unchanged//[FH2 truncatefileatoffset:0];       Truncate the contents of the file by 0 bytes (empty)//Set the read-write pointer to the end of the file [FH Seektoendoffile]; Append content to the end of the file [fh2 writedata:[@ "xxx" datausingencoding:nsutf8stringencoding]; } return 0;}

Dark Horse programmer--foundation--nsfilemanager and Nsfilehandle

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.