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

原创
2013/12/13 10:58
阅读数 1.6K
#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];
}





展开阅读全文
打赏
0
0 收藏
分享
加载中
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部