iOS文件的复制

//
//  main.m
//  FileCopy
//
//  Created by Unisk on 13-10-14.
//  Copyright (c) 2013年 Test. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    
    @autoreleasepool {
        
        NSString *srcPath=@"/Users/fdf/Desktop/BIT.pdf";
        NSString *tarPath=@"/Users/fdf/Desktop/BIT_bak.pdf";
        
        NSFileManager *fileManager=[NSFileManager defaultManager];
        BOOL success=[fileManager createFileAtPath:tarPath contents:nil attributes:nil];
        if (success) {
            NSLog(@"文件创建成功");
            
            NSFileHandle *inFile=[NSFileHandle fileHandleForReadingAtPath:srcPath];
            NSFileHandle *outFile=[NSFileHandle fileHandleForWritingAtPath:tarPath];
            
            NSDictionary   *fileAttu=[fileManager attributesOfItemAtPath:srcPath error:nil];
            NSNumber *fileSizeNum=[fileAttu objectForKey:NSFileSize];
            
            NSAutoreleasePool *pool=nil;
            int n=0;
            
            BOOL isEnd=YES;
            NSInteger readSize=0;//已经读取的数量
            NSInteger fileSize=[fileSizeNum longValue];//文件的总长度
            while (isEnd) {
                
                if (n%10==0) {
                    [pool release];
                    pool=[[NSAutoreleasePool alloc] init];
                }
                
                NSInteger subLength=fileSize-readSize;
                NSData *data=nil;
                if (subLength<5000) {
                    isEnd=NO;
                    data=[inFile readDataToEndOfFile];
                }else{
                    data=[inFile readDataOfLength:5000];
                    readSize+=5000;
                    [inFile seekToFileOffset:readSize];
                }
                [outFile writeData:data];
                n++;
            }
            
            [inFile closeFile];
            [outFile closeFile];
        }
    }
    return 0;
}

你可能感兴趣的:(ios,c)