中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

iOS 實(shí)現(xiàn)文件的拷貝

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
 #import <Foundation/Foundation.h>

//實(shí)現(xiàn)文件的拷貝

#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

//緩沖區(qū)大小

int main(int argc, const char * argv[]) {

    @autoreleasepool {


        //所謂文件拷貝 就是從原文件里讀往目的文件里寫

        //首先創(chuàng)建文件

        NSFileManager * fm =[NSFileManager defaultManager];

        NSError * error = nil;

        //獲取源文件的屬性

        NSDictionary *attributes = [fm attributesOfItemAtPath:SRCPATH error:&error];

//        PERROR(error);

        if(error){NSLog(@"%@",error);exit(-1);};

        //創(chuàng)建新文件

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

        //不要一口氣就將源文件讀入內(nèi)存

        //首先要獲取源文件大小

//        size_t size = [[attributes objectForKey:@"NSFileSize"] integerValue];

        unsigned long long size = [attributes fileSize];

        //這是一個(gè)方法,只有當(dāng)字典中裝文件屬性才有效 實(shí)際上是一個(gè)類別

        /*

         @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個(gè)字節(jié)

                data = [srcFh readDataOfLength:BUFF];

                size -= BUFF;

            }

            [dstFh writeData:data];

        }

    }

    return 0;

    }


標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Java多線程下載文件

下一篇:Java計(jì)算兩個(gè)日期相差多少天