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