码迷,mamicode.com
首页 > 其他好文 > 详细

OC基础:7.1 7.2 7.3 NSFileHandle 和 NSFileManager学习

时间:2014-08-29 14:45:28      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:os   io   ar   for   文件   div   cti   log   on   



复制一个文件:

 @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学习

标签:os   io   ar   for   文件   div   cti   log   on   

原文地址:http://blog.csdn.net/huang2009303513/article/details/38923177

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!