首页 > 代码库 > OC基础:7.1 7.2 7.3 NSFileHandle 和 NSFileManager学习
OC基础:7.1 7.2 7.3 NSFileHandle 和 NSFileManager学习
复制一个文件:
@autoreleasepool { NSString *homePah = NSHomeDirectory(); NSLog(@"%@",homePah); NSString *srcPath = [homePah stringByAppendingPathComponent:@"iOS.pdf"]; NSString *tagetPath = [homePah stringByAppendingPathComponent:@"iOS_bak.pdf"]; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL success = [fileManager createFileAtPath:tagetPath contents:nil attributes:nil]; if (success) { NSLog(@"创建成功"); } NSFileHandle *inFile = [NSFileHandle fileHandleForReadingAtPath:srcPath]; NSFileHandle *outFile = [NSFileHandle fileHandleForWritingAtPath:tagetPath]; NSDictionary *fileAttr = [fileManager attributesOfItemAtPath:srcPath error:nil]; NSNumber *fileSizeNum = [fileAttr objectForKey:NSFileSize]; BOOL isEnd = YES; NSInteger readSize = 0; NSInteger fileSize = [fileSizeNum longValue]; while (isEnd) { NSInteger subLeng = fileSize - readSize; NSData *data = nil; if (subLeng < 500) { isEnd = NO; data = [inFile readDataToEndOfFile]; }else{ data = [inFile readDataOfLength:500]; readSize += 500; [inFile seekToFileOffset:readSize]; } [outFile writeData:data]; } [outFile closeFile]; }
OC基础:7.1 7.2 7.3 NSFileHandle 和 NSFileManager学习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。