//单例类
#pragma mark - 创建对象
NSFileManager *fileManager = [NSFileManager defaultManager];
//文件路径
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"25-1.plist"];
//判断一个文件是否存在 传参路径
BOOL isExist = [fileManager fileExistsAtPath:filePath];
if (isExist)
{
NSLog(@"文件存在");
}
#pragma mark - 复制 源路径 目的路径
//程序安装后 沙盒中.app的绝对路径
//NSLog(@"bundle=====%@", [[NSBundle mainBundle] bundlePath]);
//文件的绝对路径
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"25-1" ofType:@"plist"];
//NSLog(@"resourcePath===%@", resourcePath);
//访问另外一个bundle
NSBundle *anotherBundle = [NSBundle bundleWithPath:@"~/NSFileManager 文件管理器.app/mapapi.bundle/images"];
//打印bundle的路径
NSLog(@"anotherBundlePath === %@", [anotherBundle bundlePath]);
//获取该目录下的所有文件或文件夹 传参为哪个目录
NSArray *directories = [fileManager contentsOfDirectoryAtPath:[anotherBundle bundlePath] error:nil];
NSLog(@"directories====%@", directories);
//相对路径
//UIImage *image = [UIImage imageNamed:@""];
//目的路径
NSString *destinationPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"25-1.plist"];
//如果文件不存在 复制操作
if (![fileManager fileExistsAtPath:destinationPath])
{
//复制操作
BOOL isCopy = [fileManager copyItemAtPath:resourcePath toPath:destinationPath error:nil];
if (isCopy)
NSLog(@"复制成功");
}
#pragma mark - 移动 新建文件夹 源路径 目的路径
//移动目的路径
NSString *moveDestinationPath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"25-1.plist"];
BOOL isMove = [fileManager moveItemAtPath:destinationPath toPath:moveDestinationPath error:nil];
if (isMove) NSLog(@"移动OK");
//创建文件夹路径
NSString *creatDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"a1/a2/a3"];
NSLog(@"creatDirectoryPath===%@", creatDirectoryPath);
//是否创建文件夹成功
NSLog(@"%@", [fileManager createDirectoryAtPath:creatDirectoryPath withIntermediateDirectories:NO attributes:nil error:nil]?@"创建文件夹成功":@"创建文件夹失败");
//文件新路径
NSString *newFilePath = [creatDirectoryPath stringByAppendingPathComponent:@"25-1.plist"];
//移动失败 因为创建文件夹时createIntermediates该参数为NO 获得不到路径
NSLog(@"%@", [fileManager moveItemAtPath:moveDestinationPath toPath:newFilePath error:nil]?@"移动成功":@"移动失败");
//创建文件夹
/*
第一个参数 创建文件夹路径
第二个参数 创建目的文件夹时 是否创建中间不存在的文件夹
*/
[fileManager createDirectoryAtPath:creatDirectoryPath withIntermediateDirectories:YES attributes:nil error:nil];
//再次创建文件夹 createIntermediates为YES 创建成功
NSLog(@"%@", [fileManager moveItemAtPath:moveDestinationPath toPath:newFilePath error:nil]?@"第二次移动成功":@"第二次移动失败");
//删除
BOOL isRemove = [fileManager removeItemAtPath:newFilePath
error:nil];
if (isRemove) {
NSLog(@"oh a waste of time");
}