您的位置:首页 > 移动开发 > IOS开发

iOS疯狂详解之NSFileManager使用

2015-09-15 13:26 639 查看
NSFileManager 是一个对文件进行操作的类

可以创建文件夹,移动文件夹,复制文件夹等.

// 创建文件
- (void)createFile
{
// 获取Documents文件路径
#define kDocumentsPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]

// 拼接路径
NSString *filePath = [kDocumentsPath stringByAppendingPathComponent:@"Download"];
NSLog(@"%@",filePath);
// 获取 操作文件对象
NSFileManager *fileManger = [NSFileManager defaultManager];

// withIntermediateDirectories
// YES 如果不存在 创建 可以覆盖 反之 不可以覆盖(创建失败)
BOOL isCreateFile = [fileManger createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];

NSLog(@"%d",isCreateFile);

}

// 移动
- (void)moveFile
{
// 拼接老路径
NSString *old = [kDocumentsPath stringByAppendingPathComponent:@"Download"];
// 拼接新路径
NSString *new = [kCachesPath stringByAppendingPathComponent:@"Download"];
NSFileManager *fileManger = [NSFileManager defaultManager];

BOOL isMoved = [fileManger moveItemAtPath:old toPath:new error:nil];
NSLog(@"%d",isMoved);

}
// 复制
- (void)copyFile
{
// 拼接老路径
NSString *old = [kDocumentsPath stringByAppendingPathComponent:@"Download"];
// 拼接新路径
NSString *new = [kCachesPath stringByAppendingPathComponent:@"Download"];
NSFileManager *fileManger = [NSFileManager defaultManager];

BOOL isCopy = [fileManger copyItemAtPath:new toPath:old error:nil];
NSLog(@"%d",isCopy);

}
// 删除
- (void)deleteFile
{
// 拼接新路径
NSString *new = [kCachesPath stringByAppendingPathComponent:@"Download"];
NSFileManager *fileManger = [NSFileManager defaultManager];

BOOL isDelete = [fileManger removeItemAtPath:new error:nil];
NSLog(@"%d",isDelete);

}
// 是否存在
- (void)isExistFile
{
// 拼接老路径
NSString *old = [kDocumentsPath stringByAppendingPathComponent:@"Download"];
NSFileManager *fileManger = [NSFileManager defaultManager];

BOOL isExist = [fileManger fileExistsAtPath:old];
NSLog(@"%d",isExist);
}

综上:NSFileManager这个类是一个单例类 可以对文件进行操作.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS开发 NSFileManager