oc学习之旅:NSFileHandle与NSFileManager实现FileCopy

来源:互联网 发布:java 循环打印map 编辑:程序博客网 时间:2024/05/18 02:45

#import <Foundation/Foundation.h>

#define SRCPATH @"/Users/chanbin/Desktop/release.mp4"

#define DESPATH @"/Users/chanbin/Desktop/RE.mp4"


         NSFileManager

         判断文件下是否存在

         [manager fileExistsAtPath:PATH isDirectory:<#(BOOL *)#>]

         if(文件夹不存在)

            1.创建文件夹

         {

          [manager createDirectoryAtPath:<#(NSString *)#> withIntermediateDirectories:<#(BOOL)#> attributes:<#(NSDictionary *)#> error:<#(NSError *__autoreleasing *)#>];

         }

         创建文件部分:

         [manager fileExistsAtPath:PATH]

         if(文件不存在)

            2.创建文件

         {

            [manager createFileAtPath:<#(NSString *)#> contents:<#(NSData *)#> attributes:<#(NSDictionary *)#>];

         }

         创建成功

         --------------

         NSFileHandle 获得一个可读可写的文件句柄对象 NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath:PATH];

         读写文件 操作nsdata对象

         写:wirteData

         读:readDataOfLength

         读写过程中需要关注文件流指针:决定读取写到哪个位置

         操作流指针:seekToFileOffset


int main(int argc,constchar * argv[])

{


    @autoreleasepool {

        

        NSFileManager *manager = [NSFileManagerdefaultManager];

       if (![managerfileExistsAtPath:DESPATH ]) {

            [manager createFileAtPath:DESPATHcontents:nilattributes:nil];

        }

        

       NSFileHandle *readHande = [NSFileHandlefileHandleForReadingAtPath:SRCPATH];

       NSFileHandle *writeHande = [NSFileHandlefileHandleForWritingAtPath:DESPATH];

        

        //获取文件总大小先把文件读到字典中去(官方取文件大小方法步骤1

       NSDictionary *dic = [managerattributesOfItemAtPath:SRCPATHerror:nil];

        //从字典中读取大小(官方获取文件方法步骤2

       unsignedlonglong totalSize =[[dicobjectForKey:NSFileSize]integerValue];

       unsignedlonglong readSize =0;

       while (totalSize) {

           NSData * data =nil;

            //最后不足1024字节处理

           if(totalSize /1024){

                totalSize-=1024;

                readSize +=1024;

               //数据读取

                data = [readHandereadDataOfLength:1024];

            }else{

               //不足1024字节

               unsignedlonglong size = totalSize %1024;

                totalSize -= size;

                data = [readHandereadDataOfLength:size];

            }

           //写文件

            [writeHandewriteData:data];

        }

        [readHandecloseFile];

        [writeHandecloseFile];

    }

   return0;

}


或者

#import <Foundation/Foundation.h>

#define REPATH @"/Users/chanbin/Desktop/release.mp4"

#define TOPATH @"/Users/chanbin/Desktop/RE.mp4"

int main(int argc,constchar * argv[])

{


    @autoreleasepool {

        

        NSFileManager *manager = [NSFileManagerdefaultManager];

       //新建拷贝文件

       if ([managerfileExistsAtPath:TOPATH]) {

           NSLog(@"File exit");

        }else{

            [manager createFileAtPath:TOPATHcontents:nilattributes:nil];

           //读写文件句柄对象

           NSFileHandle *handle1 = [NSFileHandlefileHandleForReadingAtPath:REPATH];

           NSFileHandle *handle2 = [NSFileHandlefileHandleForWritingAtPath:TOPATH];

           //获取文件大小

           unsignedlonglong length = [handle1seekToEndOfFile];

            [handle1seekToFileOffset:0];

           int  i ;

           for (i =0;1024* i <length; i++) {

                

               NSData *data = [handle1readDataOfLength:1024];

                [handle2writeData:data];

                [handle1seekToFileOffset:i*1024];

            }

            [handle2writeData:[handle1readDataOfLength:(length - 1024 *(i - 1))]];

            [handle2closeFile];

            [handle1closeFile];

        }

    }

   return0;

}



0 0
原创粉丝点击