oc10_ File Exercises

Source: Internet
Author: User

////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

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.