#import <Foundation/Foundation.h>
#define SRCPATH @"/Users/chanbin/Desktop/release.mp4"
#define DESPATH @"/Users/chanbin/Desktop/RE.mp4"
NSFileManager
判断文件下是否存在
[manager fileExistsAtPath:PATH isDirectory:<#(BOOL *)#>]
if(文件夹不存在)
1.创建文件夹
{
[manager createDirectoryAtPath:<#(NSString *)#> withIntermediateDirectories:<#(BOOL)#> attributes:<#(NSDictionary *)#> error:<#(NSError *__autoreleasing *)#>];
}
创建文件部分:
[manager fileExistsAtPath:PATH]
if(文件不存在)
2.创建文件
{
[manager createFileAtPath:<#(NSString *)#> contents:<#(NSData *)#> attributes:<#(NSDictionary *)#>];
}
创建成功
--------------
NSFileHandle 获得一个可读可写的文件句柄对象 NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath:PATH];
读写文件 操作nsdata对象
写:wirteData
读:readDataOfLength
读写过程中需要关注文件流指针:决定读取 写到哪个位置
操作流指针:seekToFileOffset:
int main(int argc,const char * argv[])
{
@autoreleasepool {
NSFileManager *manager = [NSFileManagerdefaultManager];
if (![manager fileExistsAtPath:DESPATH ]) {
[manager createFileAtPath:DESPATHcontents:nilattributes:nil];
}
NSFileHandle *readHande = [NSFileHandlefileHandleForReadingAtPath: SRCPATH];
NSFileHandle *writeHande = [NSFileHandlefileHandleForWritingAtPath:DESPATH];
//获取文件总大小先把文件读到字典中去(官方取文件大小方法 步骤1)
NSDictionary *dic = [manager attributesOfItemAtPath:SRCPATH error:nil];
//从字典中读取大小(官方获取文件方法 步骤2)
unsigned longlong totalSize =[[dic objectForKey:NSFileSize] integerValue];
unsigned longlong readSize = 0;
while (totalSize) {
NSData * data = nil;
//最后不足1024字节处理
if(totalSize /1024){
totalSize-=1024;
readSize +=1024;
//数据读取
data = [readHandereadDataOfLength:1024];
}else{
//不足1024字节
unsigned longlong size = totalSize %1024;
totalSize -= size;
data = [readHandereadDataOfLength:size];
}
//写文件
[writeHandewriteData:data];
}
[readHandecloseFile];
[writeHandecloseFile];
}
return 0;
}
或者
#import <Foundation/Foundation.h>
#define REPATH @"/Users/chanbin/Desktop/release.mp4"
#define TOPATH @"/Users/chanbin/Desktop/RE.mp4"
int main(int argc,const char * argv[])
{
@autoreleasepool {
NSFileManager *manager = [NSFileManagerdefaultManager];
//新建拷贝文件
if ([manager fileExistsAtPath:TOPATH]) {
NSLog(@"File exit");
}else{
[manager createFileAtPath:TOPATHcontents:nilattributes:nil];
//读写文件句柄对象
NSFileHandle *handle1 = [NSFileHandlefileHandleForReadingAtPath:REPATH];
NSFileHandle *handle2 = [NSFileHandlefileHandleForWritingAtPath:TOPATH];
//获取文件大小
unsigned longlong length = [handle1 seekToEndOfFile];
[handle1seekToFileOffset:0];
int i ;
for (i = 0;1024* i <length; i++) {
NSData *data = [handle1 readDataOfLength:1024];
[handle2writeData:data];
[handle1seekToFileOffset:i*1024];
}
[handle2writeData:[handle1 readDataOfLength:(length - 1024 *(i - 1))]];
[handle2closeFile];
[handle1closeFile];
}
}
return 0;
}