您的位置:首页 > 移动开发 > Objective-C

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

2016-02-18 00:18 441 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: