oc 文件管理NSFileManager,文件读写NSFileHandle 文件复制
2015-05-27 20:44
393 查看
oc 文件管理NSFileManager,文件读写NSFileHandle ,设计一个文件管理类将一个文件复制到另一个文件,由于文件过大要求每次复制100长度,通过NSFileHandle 来操作
设计一个文件管理类将一个文件复制到另一个文件,由于文件过大要求每次复制100长度,通过NSFileHandle来操作,并通过代理模式打印出当前的赋值进度百分main.m
#import <Foundation/Foundation.h>
#import "FileMaker.h"
int main(int argc,const char * argv[]) {
@autoreleasepool {
NSString * fromPath = [NSHomeDirectory()stringByAppendingPathComponent:@"desktop/name.txt"];
NSString * toPath = [NSHomeDirectory()stringByAppendingPathComponent:@"desktop/usa.txt"];
FileMaker * fileMaker = [[FileMakeralloc]init];
[fileMaker copyFileFromPath:fromPath toPath:toPath];
}
return 0;
}
FileMaker.m
#import "FileMaker.h"
@implementation FileMaker
设计一个文件管理类将一个文件复制到另一个文件,由于文件过大要求每次复制100长度,通过NSFileHandle来操作,并通过代理模式打印出当前的赋值进度百分比
- (void)copyFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath{
// 1.1创建文件管理类
NSFileManager * manager = [NSFileManagerdefaultManager];
// 1.2通过键获取值(字符串)并转换为int类型,这样不用通过读数据就获取了文件的长度
// 通过文件管对象方法attributesOfItemAtPath:fromPath error:nil获得文件的一个字典
NSDictionary * dict = [manager attributesOfItemAtPath:fromPath error:nil];//此方法其中提供了文件容量的键值对
int totalSize = [[dict valueForKey:@"NSFileSize" ] intValue];
//1.3创建目标文件(在没有的情况下,若果不确定加判断是否存在,存在不创建,不存在创建)
[manager createFileAtPath:toPathcontents:nilattributes:nil];
NSLog(@"%@",dict);
// 2. 分别创建读、写管理者
NSFileHandle * readHandle = [NSFileHandlefileHandleForReadingAtPath:fromPath];
NSFileHandle * writeHandle = [NSFileHandlefileHandleForWritingAtPath:toPath];
设计一个文件管理类将一个文件复制到另一个文件,由于文件过大要求每次复制100长度,通过NSFileHandle来操作,并通过代理模式打印出当前的赋值进度百分比
// 3.循环读取源文件,并且写入目标文件
int per = 100;
int times = totalSize%100 ==0 ? totalSize/per :totalSize/per +1;
for (int i =0; i < times ; i++) {
[readHandleseekToFileOffset:per * i];
NSData * data = [readHandle readDataOfLength:per];
[writeHandleseekToEndOfFile];
[writeHandle writeData:data];
}
// 关闭文件
[writeHandlecloseFile];
[readHandlecloseFile];
}
@end
相关文章推荐
- oc 文件管理NSFileManager,文件读写NSFileHandle ,设计一个文件管理类将一个文件复制到另一个文件,由于文件过大要求每次复制100长度,通过NSFileHandle 来操作,并
- OC文件管理NSFileManager与文件读写NSFileHandle
- 【iOS】第03讲 文件读写/NSArray/NSData/NSFileManager/NSFileHandle
- oc学习之旅:NSFileHandle与NSFileManager实现FileCopy
- 一道题看懂OC的文件管理:NSFileManager,计算文件包含内存大小
- OC文件管理NSFileManager与文件读写NSFileHandle
- IO-文件 File 复制 读写 总结
- 09-OC中NSArray的文件读写(File/NSURL)
- Java File类文件管理及IO读写、复制操作
- OC中关于NSFileManager判断是否存在某一个文件
- Windows8:文件管理---File创建和String Stream Buffer方式读写
- 【Java】File类文件管理及IO读写、复制操作
- 文件内容操作类:NSFileHandle
- 基本文件操作:NSFileHandle的用法
- UIday1801:沙盒三个文件的路径的获取、简单与复杂对象的读写、NSUserDefaults、NSFileManager
- Windows 8实用窍门系列:12.windows 8的文件管理---1.File创建和String Stream Buffer方式读写
- 基本文件操作:NSFileHandle的用法
- Java学习笔记——File类文件管理及IO读写、复制操作
- NSFileHandle & NSFileManager 文件复制例程
- 【Foundation】NSFileHandle 文件操作柄