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

时间:2014-08-29 14:45:28   收藏:0   阅读:274



复制一个文件:

 @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];
    }


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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!