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
#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
相关文章推荐
- LBS-Object C 定位,地图,导航
- NSObject
- Object-C self = [super init]
- Objective-C中的instancetype和id区别
- How Do I Declare A Block in Objective-C?
- Objective-C 相关Category2
- Objective-C 相关Category
- JS入门之ActiveXObject对象
- java JSONObject对象序列化与反序列化
- 使用objection来模块化开发iOS项目
- Objective-C 11 属性property
- 《转》Objective-C Runtime(4)- 成员变量与属性
- 《转》Objective-C Runtime(3)- 消息 和 Category
- 《转》Objective-C Runtime(2)- Object & Class & Meta Class
- 《转》Objective-C Runtime(1)- Self & Super
- 使用ObjectAnimator设置动画
- Objective-C中Self和Super详解
- objective-C学习 谓词NSPredicate 可过滤
- 关于MarshalByRefObject的解释
- CreateMutex、WaitForSingleObject、ReleaseMutex——创建互斥对象