objective-C学习 KVO 监听文件读取进度

#import <Foundation/Foundation.h>

#import "User.h"


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

    @autoreleasepool {


        User *user = [[User alloc]init];

        [user doCopy];

        

    }

    return 0;

}



#import <Foundation/Foundation.h>

#import "FileHandle.h"


@interface User : NSObject{


@private

    FileHandle *fileHandle;

    

}


- (void)doCopy;


@end




#import "User.h"


@implementation User


- (id)init{

    self = [super init];

    if (self != nil) {

        

        NSString *homePath = NSHomeDirectory();

        NSString *srcPath = [homePath stringByAppendingPathComponent:@"chuzj.txt"];  //原始路径

        NSString *tagetPath = [homePath stringByAppendingPathComponent:@"chuzjcopy.txt"]; //目标路径

        

        fileHandle = [[FileHandle alloc]initPath:srcPath tagetPath:tagetPath];

        

        [fileHandle addObserver:self forKeyPath:@"readedsize" options:NSKeyValueObservingOptionNew context:nil];

    }


    return self;


}


- (void)doCopy{

    [fileHandle startCopy];

}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{


    if ([keyPath isEqualToString:@"readedsize"]) {

        NSNumber *readsizeNum = [change objectForKey:@"new"];

        float readsize = [readsizeNum floatValue];

        float filesize = fileHandle.filesize;

        float ret = readsize /filesize * 100;

        NSLog(@"ret = %.1f",ret);

    }

}


@end




#import <Foundation/Foundation.h>


@interface FileHandle : NSObject{


@private

    NSString *_srcPath;    //源文件路径

    NSString *_tagetPath;    //目标文件路径

}


@property (nonatomic,assign)float filesize;   //文件总大小

@property (nonatomic,assign)float readedsize;   //读取文件大小


- (id)initPath:(NSString *)srcPath tagetPath:(NSString *)tagetPath;


- (void)startCopy;


@end




#import "FileHandle.h"


@implementation FileHandle


- (id)initPath:(NSString *)srcPath tagetPath:(NSString *)tagetPath{

    self = [super init];

    if (self != nil) {

        _srcPath = [srcPath copy];

        _tagetPath = [tagetPath copy];

    }

    return self;

}


- (void)startCopy{

    

    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL success = [fileManager createFileAtPath:_tagetPath contents:nil attributes:nil];

    if (success) {

        NSLog(@"create success !");

    }

    

    NSFileHandle *inFile = [NSFileHandle fileHandleForReadingAtPath:_srcPath];

    NSFileHandle *outFile = [NSFileHandle fileHandleForWritingAtPath:_tagetPath];

    

    //获取原始文件长度

    NSDictionary *fileAtt = [fileManager attributesOfItemAtPath:_srcPath error:nil];

    NSNumber *fileSizeNum = [fileAtt objectForKey:NSFileSize];

   self.filesize = [fileSizeNum longValue];

    NSLog(@"fileSize = %ld",self.filesize);

    

    BOOL isEnd = YES;

    NSLog(@"开始复制-----");

    while (isEnd) {

        

        NSInteger subLength = _filesize - _readedsize;

        NSData *data;

        if (subLength < 500) {

            data = [inFile readDataToEndOfFile];     //读到结尾

            isEnd = NO;     //跳出循环

            

        } else {

            data = [inFile readDataOfLength:500];    //读出500个字节

//            readSize +=500;

            self.readedsize +=500;

            [inFile seekToFileOffset:_readedsize];  //移动偏移量

        }

        [outFile writeData:data];  //文件写入

    }  

    [outFile closeFile];

    NSLog(@"文件复制成功!");

}


@end




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值