////TextHander.h//oc10_ File Exercises////Created by zhangxueming on 15/6/19.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import<Foundation/Foundation.h>@interfaceTexthander:nsobject+ (void) Replacestringintextfile: (NSString *) path witholdstring: (NSString *) dststring tonewstring: (NSString *) newstring;+ (void) Replacestringindirectory: (NSString *) path witholdstring: (NSString *) dststring tonewstring: (NSString *) newString FileType: (NSString *) type;@end
////TEXTHANDER.M//oc10_ File Exercises////Created by zhangxueming on 15/6/19.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import "TextHander.h"@implementationTexthander+ (void) Replacestringintextfile: (NSString *) path witholdstring: (NSString *) dststring tonewstring: (NSString *) newstring{//backing up files/name/name_bakNsfilemanager *FM =[Nsfilemanager Defaultmanager]; [FM Copyitematpath:path topath:[path stringbyappendingstring:@"_bak"] Error:nil]; //1. Create a file handleNsfilehandle *FH =[Nsfilehandle Filehandleforupdatingatpath:path]; //2. Reading filesNSData *data =[FH Readdatatoendoffile]; NSString*datastring =[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; //3. Replace contentDatastring =[datastring stringbyreplacingoccurrencesofstring:dststring withstring:newstring]; //4. File Empty[FH Truncatefileatoffset:0]; //5. Writing Files[FH writedata:[datastring datausingencoding:nsutf8stringencoding]; //6. Synchronizing Files[FH Synchronizefile]; //7. Close the file[FH CloseFile];}+ (void) Replacestringindirectory: (NSString *) path witholdstring: (NSString *) dststring tonewstring: (NSString *) newString FileType: (NSString *) type{//1. Traversing the directoryNsfilemanager *FM =[Nsfilemanager Defaultmanager]; Nsarray*contents =[FM Subpathsofdirectoryatpath:path Error:nil]; ///dic/filename.txt//2. Traversing an array to find a file of the specified type for(NSString *iteminchcontents) {NSString*name =[item pathextension]; if([name Isequaltostring:type]) {[Self Replacestringintextfile:[path stringbyappendingformat:@"/%@", item] witholdstring:dststring tonewstring:newstring]; } }}@end
////main.m//oc10_ File Exercises////Created by zhangxueming on 15/6/19.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import<Foundation/Foundation.h>#import "TextHander.h"//Replace the string "him" in the file specified by path with "Me"//[Lzxtexthandle replacestringintextfile:file_path witholdstring:@ "him" tonewstring:@ "Me"];//The following code works://Replace the string "him" in the TXT file in the folder specified by path with the string "Me"//[Lzxtexthandle replacestringindirectory:dir_path witholdstring:@ "him" tonewstring:@ "Me" fileType:@ "txt"];intMainintargcConst Char*argv[]) {@autoreleasepool {//Insert code here ...//NSLog (@ "Hello, world!");//[Texthander replacestringintextfile:@ "/users/zhangxueming/desktop/test/3.txt" witholdstring:@ "him" tonewstring:@ "Me"];[Texthander replacestringindirectory:@"/users/zhangxueming/desktop/test"Witholdstring:@"him"Tonewstring:@"Me"FileType:@"txt"]; } return 0;}
oc10_ File Exercises