//初始化文件管理对象,单例对象
self.manager = [NSFileManager defaultManager];
//拼接两个路径xx/Documents/test/test01.txt; test02.txt
NSString *firstFilePath = [self.testPath stringByAppendingPathComponent:@"test01.txt"];
//1.创建文件夹的方法
//第二参数:是否需要一个中间的文件夹(YES:允许该文件夹已经存在; NO:不允许该文件夹存在)
第三参数(扩展):指定文件夹的权限(nil:使用默认的权限:rwx:读/写/执行)
NSError *error = nil;
[self.manager createDirectoryAtPath:self.testPath withIntermediateDirectories:YES attributes:nil error:&error];
//2.创建txt文件并写入数据的方法
//第二个参数: 写入文件的内容(二进制类型),如果写nil表示创建空文件
第三个参数(扩展): 指定文件的权限(nil:使用默认的文件权限)
注意:首先会判断文件是否存在,存在,直接写入内容; 不存在,重新创建,并写入内容
BOOL isSuccess = [self.manager createFileAtPath:firstFilePath contents:[firstContent dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
//3.执行拷贝的动作
BOOL isSuccess = [self.manager copyItemAtPath:sourcePath toPath:targetPath error:&error];
//4.文件的删除
BOOL isSuccess = [self.manager removeItemAtPath:copyPath error:&error]
//5.文件的剪切(移动)
BOOLisSuccess = [self.managermoveItemAtPath:sourcePathtoPath:targetPath error:&error];
6.获取文件夹下的所有文件
//方式一、获取/test下面的所有文件(test01.txt; test02.txt)
NSArray *subPathArray = [self.manager subpathsAtPath:self.testPath];
//方式二:获取test文件夹下面的所有文件
NSArray *subPathDirArray = [self.managersubpathsOfDirectoryAtPath:self.testPatherror:nil];
//判断是否存在目录
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:cachesPath];
七、句柄NSFileHandle
1.创建读句柄
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:self.filePath];
2.创建写句柄
NSFileHandle *writeHandle = [NSFileHandle fileHandleForWritingAtPath:targetPath];
3.执行真正的拷贝动作读 取数据
NSData *readData = [readHandle readDataToEndOfFile];
4.写句柄写入数据
[writeHandle writeData:readData];
4.关闭两个句柄对象
[readHandle closeFile];
[writeHandle closeFile];
例子:
//准备工作
NSString *sourcePath = [self.documentsPath stringByAppendingPathComponent:@"bigFile.pdf"];
//1.创建一个空的目标文件bigFileBak.pdf
NSString *targetPath = [self.documentsPath stringByAppendingPathComponent:@"bigFileBak.pdf"];
BOOL isSuccess = [self.manager createFileAtPath:targetPath contents:nil attributes:nil];
if (isSuccess) {
//2.创建两个文件句柄(源:读句柄;目标:写句柄)
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:sourcePath];
NSFileHandle *writeHandle = [NSFileHandle fileHandleForWritingAtPath:targetPath];
//3.从源文件中读数据;写到目标文件中
//3.1 循环读取源文件的数据
//3.2 判断是不是指定读取数据大小的整数倍
//3.3 文件总长度
NSError *error = nil;
NSDictionary *sourceFileDic = [self.manager attributesOfItemAtPath:sourcePath error:&error];
NSLog(@"文件的属性:%@", sourceFileDic);
NSNumber *fileSizeNumber = [sourceFileDic objectForKey:NSFileSize];
NSInteger fileTotalSize = [fileSizeNumber longValue];
//声明几个常量
NSInteger readSizePerTime = 5000;//每次读取数据大小(bytes)
NSInteger readSize = 0;//已经读取数据大小
BOOL isEnd = YES;//是否循环结束
//循环读取数据
while (isEnd) {
//还剩余没有读取的数据大小
NSInteger leftSize = fileTotalSize - readSize;
if (leftSize < readSizePerTime) {
//情况一: 不足5000
NSData *readFileData = [readHandle readDataToEndOfFile];
//写入目标文件
[writeHandle writeData:readFileData];
//设置while退出的条件
isEnd = NO;
} else {
//情况二: 5000整数倍
//读取/设置readSize/挪动文件句柄
NSData *readFileData = [readHandle readDataOfLength:readSizePerTime];
readSize += readSizePerTime;
[readHandle seekToFileOffset:readSize];
//写到目标文件中
[writeHandle writeData:readFileData];
}
}
//4.关闭两个文件句柄
[readHandle closeFile];
[writeHandle closeFile];
}