NSFileHandle & NSFileManager 文件复制例程
2013-08-13 23:15
531 查看
/*
* 复制一个大型文件,为了节约内存,每次只读取500字节。
*/
#import <Foundation/Foundation.h>
int main(int argc,
const char * argv[])
{
@autoreleasepool {
//
获取当前用户 路径 /Users/xxh
NSString *homePath =
NSHomeDirectory();
// 源文件
NSString *srcPath = [homePath
stringByAppendingPathComponent:@"Desktop/Cocos2D_Projects_v2.0.0.zip"];
// 目标文件
NSString *tagetPath = [homePath
stringByAppendingPathComponent:@"Desktop/Cocos2D_Projects_v2.0.0_back.zip"];
NSLog(@"targetPaht : %@", tagetPath);
//
创建文件管理对象
NSFileManager *fileManager = [NSFileManager
defaultManager];
// 新建文件
BOOL success = [fileManager
createFileAtPath:tagetPath contents:nil
attributes:nil];
if (success) {
NSLog(@"create success!");
}
//
文件读操作对象
NSFileHandle *inFileHandle = [NSFileHandle
fileHandleForReadingAtPath:srcPath];
//
文件写操作对象
NSFileHandle *outFileHandle = [NSFileHandle
fileHandleForWritingAtPath:tagetPath];
/*
* 注意:当获取文件长度时
* 此方法已经把整个文件读取到内存中,因此就没必要在分段读取数据到内存。
NSData *data = [inFileHandle availableData];
NSInteger fileSize = data.length;
*/
//
通过文件属性获取文件长度
NSDictionary *fileAttribute = [fileManager
attributesOfFileSystemForPath:srcPath error:nil];
NSNumber *fileSizeNum = [fileAttribute
objectForKey:NSFileSize];
NSInteger fileSize = [fileSizeNum
longValue];
BOOL isEnd = YES;
NSInteger readSize =
0; // 读取文件的字节数
while (isEnd) {
NSInteger subLength = fileSize - readSize;
NSData *data = nil;
if (subLength < 500) {
isEnd =
NO;
data = [inFileHandle
readDataToEndOfFile];
}
else
{
data = [inFileHandle
readDataOfLength:500];
readSize +=
500;
[inFileHandle
seekToFileOffset:500];
}
[outFileHandle
writeData:data];
}
[inFileHandle
closeFile];
[outFileHandle
closeFile];
}
return 0;
}
* 复制一个大型文件,为了节约内存,每次只读取500字节。
*/
#import <Foundation/Foundation.h>
int main(int argc,
const char * argv[])
{
@autoreleasepool {
//
获取当前用户 路径 /Users/xxh
NSString *homePath =
NSHomeDirectory();
// 源文件
NSString *srcPath = [homePath
stringByAppendingPathComponent:@"Desktop/Cocos2D_Projects_v2.0.0.zip"];
// 目标文件
NSString *tagetPath = [homePath
stringByAppendingPathComponent:@"Desktop/Cocos2D_Projects_v2.0.0_back.zip"];
NSLog(@"targetPaht : %@", tagetPath);
//
创建文件管理对象
NSFileManager *fileManager = [NSFileManager
defaultManager];
// 新建文件
BOOL success = [fileManager
createFileAtPath:tagetPath contents:nil
attributes:nil];
if (success) {
NSLog(@"create success!");
}
//
文件读操作对象
NSFileHandle *inFileHandle = [NSFileHandle
fileHandleForReadingAtPath:srcPath];
//
文件写操作对象
NSFileHandle *outFileHandle = [NSFileHandle
fileHandleForWritingAtPath:tagetPath];
/*
* 注意:当获取文件长度时
* 此方法已经把整个文件读取到内存中,因此就没必要在分段读取数据到内存。
NSData *data = [inFileHandle availableData];
NSInteger fileSize = data.length;
*/
//
通过文件属性获取文件长度
NSDictionary *fileAttribute = [fileManager
attributesOfFileSystemForPath:srcPath error:nil];
NSNumber *fileSizeNum = [fileAttribute
objectForKey:NSFileSize];
NSInteger fileSize = [fileSizeNum
longValue];
BOOL isEnd = YES;
NSInteger readSize =
0; // 读取文件的字节数
while (isEnd) {
NSInteger subLength = fileSize - readSize;
NSData *data = nil;
if (subLength < 500) {
isEnd =
NO;
data = [inFileHandle
readDataToEndOfFile];
}
else
{
data = [inFileHandle
readDataOfLength:500];
readSize +=
500;
[inFileHandle
seekToFileOffset:500];
}
[outFileHandle
writeData:data];
}
[inFileHandle
closeFile];
[outFileHandle
closeFile];
}
return 0;
}
相关文章推荐
- NSFileHandle & NSFileManager 文件创建及写入例程
- oc 文件管理NSFileManager,文件读写NSFileHandle ,设计一个文件管理类将一个文件复制到另一个文件,由于文件过大要求每次复制100长度,通过NSFileHandle 来操作,并
- oc 文件管理NSFileManager,文件读写NSFileHandle 文件复制
- NSFileManager和NSFileHandle文件的所有操作
- 【IOS 开发学习总结-OC-36】★文件 I/O——NSFileHandle&NSURL&NSBundle
- 数据持久化-存取方式总结&应用沙盒&文件管理NSFileManager
- NSFileHandle拷贝大文件+多线程
- OC-NSFileManager和NSFileHandle的使用
- mysql表复制文件过去以后提示 Mysql table 'xxx' doesn`t exist
- Python 处理文件例程
- experiment : 基于wdk's cancelsafe 例程, 拒绝文件创建
- OC基础:7.1 7.2 7.3 NSFileHandle 和 NSFileManager学习
- 数据库备份 Mysql table '' doesn`t exist .(mysql数据库复制迁移时除了复制文件夹外还要复制data目录下的ibdata1文件)
- 解决建站时或在VS.NET中浏览网页时出现"无法在***已经存在的情况下创建,影像复制该文件"的错误
- 文件管理者NSFileManager && 写入文件存储 &&字符串操作 && NSInputStream
- 安装IIS时出现"安装程序无法复制文件zClientm.exe"
- iOS——文件操作NSFileManager (创建、删除,复制,粘贴)
- Java——FileInputStream&FileOutputStream字节流实现文件复制
- NSFileManager和NSFileHandle的注意事项和常用操作归纳
- 基本文件操作&fopen函数&FILE指针&ftell &fseek&rewind&fgets文件复制&feof