标签:
NSFileManager是什么?
通熟易懂的解释:NSFileManager是文件管理者,可以
1.能够创建文件夹, 创建 删除 赋值 移动
2.通过路径查找和拼接
#pragma mark --NSFileManager 文件管理者
/**
* 1.能够创建文件夹, 创建 删除 赋值 移动
2.通过路径
3.
*/
-(void)initData{
//文件管理者都是系统的一个单例对象
NSFileManager *fileManager = [NSFileManager defaultManager];
//沙盒documents文件夹路径
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//沙盒caches文件夹路径
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//1.创建文件夹
NSString *path = [documentsPath stringByAppendingPathComponent:@"DownLoad/image"];
/*
方法的调用者:文件管理者
参数1:想要创建文件夹的路径
参数2:YES(该路径下创建不存在的文件夹) NO(忽略不存在文件夹,不创建)
参数3:对所创建的文件夹的相关设置
参数4:错误信息
*/
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
NSLog(@"%@",path);
//2.判断是否存在某个文件,如果存在,删掉,不存在则创建
NSString *filePath = [cachesPath stringByAppendingPathComponent:@"xiaopingguo.mp3"];
//判断该路径下是否存在对应的文件
if ([fileManager fileExistsAtPath:filePath]) {
//存在删除
[fileManager removeItemAtPath:filePath error:nil];
NSLog(@"文档存在,删除");
}
else
{
//不存在 创建
[fileManager createFileAtPath:filePath contents:nil attributes:nil];
NSLog(@"文件不存在,创建");
}
NSLog(@"%@",filePath);
//3.赋值
[fileManager copyItemAtPath:filePath toPath:[documentsPath stringByAppendingPathComponent:@"xiaopingguo.mp3"] error:nil];
NSLog(@"%@",documentsPath);
//4.移动
/*
参数1:文件的源路径
参数2:目的路径,需要拼接上文件名字
参数3:错误信息
*/
[fileManager moveItemAtPath:filePath toPath:[path stringByAppendingPathComponent:@"dabaojian.mp4"] error:nil];
//5.获取文件的子文件
NSArray *fileArray = [fileManager contentsOfDirectoryAtPath:documentsPath error:nil];
NSLog(@"%@",fileArray);
}标签:
原文地址:http://blog.csdn.net/y_csdnblog_xx/article/details/51361410