文件管理器 在沙盒中移动 复制 删除 文件
来源:互联网 发布:西装面料品牌 知乎 编辑:程序博客网 时间:2024/04/19 23:49
//单例类
#pragma mark - 创建对象
NSFileManager *fileManager = [NSFileManagerdefaultManager];
//文件路径
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 = [[NSBundlemainBundle] pathForResource:@"25-1"ofType:@"plist"];
//NSLog(@"resourcePath===%@", resourcePath);
//访问另外一个bundle
NSBundle *anotherBundle = [NSBundlebundleWithPath:@"~/NSFileManager文件管理器.app/mapapi.bundle/images"];
//打印bundle的路径
NSLog(@"anotherBundlePath === %@", [anotherBundlebundlePath]);
//获取该目录下的所有文件或文件夹传参为哪个目录
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:destinationPatherror:nil];
if (isCopy)
NSLog(@"复制成功");
}
#pragma mark - 移动 新建文件夹 源路径 目的路径
//移动目的路径
NSString *moveDestinationPath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"25-1.plist"];
BOOL isMove = [fileManager moveItemAtPath:destinationPath toPath:moveDestinationPatherror:nil];
if (isMove) NSLog(@"移动OK");
//创建文件夹路径
NSString *creatDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"a1/a2/a3"];
NSLog(@"creatDirectoryPath===%@", creatDirectoryPath);
//是否创建文件夹成功
NSLog(@"%@", [fileManagercreateDirectoryAtPath:creatDirectoryPath withIntermediateDirectories:NOattributes:nilerror:nil]?@"创建文件夹成功":@"创建文件夹失败");
//文件新路径
NSString *newFilePath = [creatDirectoryPath stringByAppendingPathComponent:@"25-1.plist"];
//移动失败因为创建文件夹时createIntermediates该参数为NO获得不到路径
NSLog(@"%@", [fileManagermoveItemAtPath:moveDestinationPath toPath:newFilePath error:nil]?@"移动成功":@"移动失败");
//创建文件夹
/*
第一个参数创建文件夹路径
第二个参数创建目的文件夹时 是否创建中间不存在的文件夹
*/
[fileManager createDirectoryAtPath:creatDirectoryPathwithIntermediateDirectories:YESattributes:nilerror:nil];
//再次创建文件夹 createIntermediates为YES创建成功
NSLog(@"%@", [fileManagermoveItemAtPath:moveDestinationPath toPath:newFilePath error:nil]?@"第二次移动成功":@"第二次移动失败");
//删除
BOOL isRemove = [fileManager removeItemAtPath:newFilePath
error:nil];
if (isRemove) {
NSLog(@"oh a waste of time");
}
- 文件管理器 在沙盒中移动 复制 删除 文件
- Android文件管理器开发中对文件的复制,移动,删除,新建文件夹等的操作
- 文件创建、复制、移动、删除
- VC 移动,复制,删除文件
- VC 移动,复制,删除文件
- 文件创建、复制、移动、删除
- SDK复制删除移动文件
- 文件移动、复制、粘贴、删除
- 文件创建、复制、移动、删除
- java复制文件删除文件 移动文件 复制文件夹等
- [API学习]删除、复制、重命名、移动文件
- VC 移动,复制,删除文件(SHFileOperation)
- 复制、删除和移动文件和文件夹
- VC 复制移动删除重命名文件文件夹
- VC 移动,复制,删除文件(SHFileOperation)
- java 文件新建、移动、复制、删除
- VC 复制移动删除重命名文件文件夹 .
- VC 移动,复制,删除文件(SHFileOperation)
- 芯客-Rayeager PX2应用之USB摄像头
- 2015/1/26实验室会议
- 经验总结43--新旅程
- COCOS2D-X 停止手打所有cpp文件到android.mk
- 关于调用系统相机以及压缩照片
- 文件管理器 在沙盒中移动 复制 删除 文件
- Java线程的5个使用技巧
- 内核下按行读文件方法
- PyUnit报告整理
- Codeforces Round #287 (Div. 2)(A,B题)
- 5.Spring_Bean之间的关系
- CoDeSys二次开发-OEM组件
- 20150124 【 ARM 】 C语言+指针
- 操作系统——存储管理(2)