
下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
#import <Foundation/Foundation.h>//实现文件的拷贝#define SRCPATH @"/Users/guoyule/Desktop/emailreceipt_20150214R3887454299_new.pdf"#define DSTPATH @"/Users/guoyule/Desktop/emailreceipt_20150214R3887454299_new1.pdf"#define PERROR (error) if(error){NSLog(@"%@",error);exit(-1);}#define BUFF 100//缓冲区大小int main(int argc,const char * argv[]) { @autoreleasepool { //所谓文件拷贝 就是从原文件里读往目的文件里写 //首先创建文件 NSfileManager * fm =[NSfileManager defaultManager]; NSError * error = nil; //获取源文件的属性 NSDictionary *attributes = [fm attributesOfItemAtPath:SRCPATH error:&error];// PERROR(error); if(error){NSLog(@"%@",error);exit(-1);}; //创建新文件 BOol ret = [fm createfileAtPath:DSTPATH contents:nil attributes:attributes]; if (!ret) { perror("createfile"); exit(-1); } //打开文件句柄 NSfileHandle * srcFh = [NSfileHandle fileHandleForReadingAtPath:SRCPATH]; NSfileHandle * dstFh = [NSfileHandle fileHandleForWritingAtPath:DSTPATH]; //不要一口气就将源文件读入内存 //首先要获取源文件大小// size_t size = [[attributes objectForKey:@"NSfileSize"] integerValue]; unsigned long long size = [attributes fileSize]; //这是一个方法,只有当字典中装文件属性才有效 实际上是一个类别 /* @interface NSDictionary (NSfileAttributes) - (unsigned long long)fileSize; - (NSDate *)fileModificationDate; - (Nsstring *)fileType; - (NSUInteger)filePosixPermissions; - (Nsstring *)fileOwnerAccountname; - (Nsstring *)fileGroupOwnerAccountname; - (NSInteger)fileSystemNumber; - (NSUInteger)fileSystemfileNumber; - (BOol)fileExtensionHIDden; - (OSType)fileHFSCreatorCode; - (OSType)fileHFSTypeCode; - (BOol)fileIsImmutable; - (BOol)fileIsAppendOnly; - (NSDate *)fileCreationDate; - (NSNumber *)fileOwnerAccountID; - (NSNumber *)fileGroupOwnerAccountID; @end */ while (size) { NSData * data = nil; if (size <= BUFF) { data = [srcFh readDataToEndOffile]; size = 0; }else{ //先读100个字节 data = [srcFh readDataOfLength:BUFF]; size -= BUFF; } [dstFh writeData:data]; } } return 0; } 以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的iOS 实现文件的拷贝全部内容,希望文章能够帮你解决iOS 实现文件的拷贝所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)