iOS 實現檔案的拷貝

來源:互聯網
上載者:User

標籤:

//

//  main.m

//  filecopy

//

//  Created by GuoYule on 15/2/19.

//  Copyright (c) 2015年 GuoYule. All rights reserved.

//

 

#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:SRCPATHerror:&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 = [NSFileHandlefileHandleForReadingAtPath: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;

    }

 

iOS 實現檔案的拷貝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.