Working with files in Object-c

Source: Internet
Author: User

1NSString *filepath =@"/users/scjy/hello/iphone.txt";2         3Nsfilehandle *handle=[nsfilehandle Filehandleforupdatingatpath:filepath];//Prepare for Updates4         5[Handle seektoendoffile];//read to end of file6         7NSString *addstring=@"Shang Education";8         9NSData *data=[addstring datausingencoding:nsutf8stringencoding];//converting a string into dataTen          One[Handle writedata:data];//Write Data A          -[Handle closefile];//Close File -          the          -         //Read -Nsfilehandle *handle1 =[Nsfilehandle Filehandleforreadingatpath:filepath]; -NSData *data1 =[Handle1 readdatatoendoffile]; +NSString *readstring=[[NSString alloc]initwithdata:data1 encoding:nsutf8stringencoding]; -NSLog (@"%@", readString);
View Code
1 //Copying Files2         3         4         //find the original file path5NSString *old_filepath =@"/users/scjy/hello/iphone.txt";6         7         //Specify a new file path (file does not exist)8NSString *new_filepath =@"/users/scjy/hello/ios.txt";9 Ten         //Create a File manager to prepare for creating a new file OneNsfilemanager *filemanager=[Nsfilemanager Defaultmanager]; A          -         //Create a new file (no data in the file at this time) -BOOL issuccess=[FileManager Createfileatpath:new_filepath contents:nil Attributes:nil]; the         if(issuccess) { -NSLog (@"created successfully"); -              -             //1. Read the data in the original file: Filehandleforreadingatpath +Nsfilehandle *old_handle=[Nsfilehandle Filehandleforreadingatpath:old_filepath]; -NSData *old_data=[Old_handle readdatatoendoffile]; +              A             //2. Write the data obtained from the original file to a new file: Filehandleforwritingatpath atNsfilehandle *new_handle=[Nsfilehandle Filehandleforwritingatpath:new_filepath]; - [New_handle Writedata:old_data]; -              - [Old_handle CloseFile]; - [New_handle CloseFile]; -              in         } -         Else to         { +NSLog (@"creation failed"); -}
View Code

Working with files in Object-c

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.