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

iOS 创建目录,复制文件,文件是否存在

2013-12-13 10:58 513 查看
#define FileName @"unKnowName"

- (void)createFileDirectories {    // 判断存放mid、mov的文件夹是否存在,不存在则创建对应文件夹
NSString *midPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"mid"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = FALSE;
BOOL isDirExist = [fileManager fileExistsAtPath:midPath isDirectory:&isDir];
if(!(isDirExist && isDir))    {
BOOL bCreateDir = [fileManager createDirectoryAtPath:midPath withIntermediateDirectories:YES attributes:nil error:nil];
if(!bCreateDir){
NSLog(@"Create Mid Directory Failed.");
}
NSLog(@"%@",midPath);
}
NSString *movPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"mov"];
isDir = FALSE;
isDirExist = [fileManager fileExistsAtPath:movPath isDirectory:&isDir];
if(!(isDirExist && isDir)){
BOOL bCreateDir = [fileManager createDirectoryAtPath:movPath withIntermediateDirectories:YES attributes:nil error:nil];
if(!bCreateDir){
NSLog(@"Create Mov Directory Failed.");
}
NSLog(@"%@",movPath);
}
}

- (void)copyItemToDocument {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *midPath = [[documentsDirectory stringByAppendingPathComponent:@"mid"] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mid", FileName]];

NSString *resourcePath = [[NSBundle mainBundle] pathForResource:FileName ofType:@"mid"];
if([fileManager fileExistsAtPath:midPath] == NO && midPath && resourcePath) {
[fileManager copyItemAtPath:resourcePath toPath:midPath error:nil];
}

NSString *movPath = [[documentsDirectory stringByAppendingPathComponent:@"mov"] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mov", FileName]];

resourcePath = [[NSBundle mainBundle] pathForResource:FileName ofType:@"mov"];
if([fileManager fileExistsAtPath:movPath] == NO && movPath && resourcePath) {
[fileManager copyItemAtPath:resourcePath toPath:movPath error:nil];
}
}


- (BOOL)fileExistWithName:(NSString *)fileName {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *midPath = [[documentsDirectory stringByAppendingPathComponent:@"mid"] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mid", FileName]];

NSString *movPath = [[documentsDirectory stringByAppendingPathComponent:@"mov"] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mov", FileName]];

return [fileManager fileExistsAtPath:midPath] && [fileManager fileExistsAtPath:movPath];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: