[Object C]_[初级]_[创建文件路径时,已存在同名目录和文件名的问题的解决方案]

来源:互联网 发布:post json数据 编辑:程序博客网 时间:2024/05/16 08:11

场景:导出数据到本地,以文件夹格式和文件名格式导出,数据中存在同名问题,为了防止不覆盖,要创建一个新的文件夹或者文件名用以区分同名的不同数据。

下面编写一个小例子进行说明:

DirAndFile.h

#import <Foundation/Foundation.h>@interface DirAndFile : NSObject+(NSString*) getDirPathNewName:(NSString*)path;+(NSString*) getFilePathNewName:(NSString*)path withPosfix:(NSString*)posfix;@end

DirAndFile.m

#import "DirAndFile.h"@implementation DirAndFile//路径中存在同名目录,创建一个新的目录名+(NSString*) getDirPathNewName:(NSString*)path{        NSFileManager* manager = [NSFileManager defaultManager];        NSString* tempPath =path;    NSString* tempDirPath =@"";    NSRange range = [path rangeOfString:@"/" options:NSBackwardsSearch];    range.length = range.location;    range.location = 0;    tempDirPath = [path substringWithRange:range];        //获取目录最后一个目录名称    NSString* name=[path lastPathComponent];        int count = 1;    BOOL isDir =NO;        while (true)    {        // 判断目录是否存在        BOOL isExistDir =[manager fileExistsAtPath:tempPath isDirectory:&isDir];        if (isDir && isExistDir)        {            if ([name rangeOfString:@"(" options:NSCaseInsensitiveSearch].location != NSNotFound)            {                NSRange range = [name rangeOfString:@"(" options:NSBackwardsSearch];                range.length = range.location;                range.location = 0;                name = [name substringWithRange:range];            }            name =[name stringByAppendingFormat:@"(%d)",count];            tempPath =[tempDirPath stringByAppendingPathComponent:name];            isDir =NO;            ++count;                    }        else        {            break;        }            }    return tempPath;}//路径中存在同名文件名,创建一个新的文件名+(NSString*) getFilePathNewName:(NSString*)path withPosfix:(NSString*)posfix{    NSAutoreleasePool *pool = [NSAutoreleasePool new];    NSFileManager* manager = [NSFileManager defaultManager];    int count = 1;        NSRange range = [path rangeOfString:@"." options:NSBackwardsSearch];    range.length = range.location;    range.location = 0;    NSString* namePart = [path substringWithRange:range];    NSString* temp = namePart;    while (YES)    {        temp = [temp stringByAppendingString:posfix];        if (![manager fileExistsAtPath:temp])        {            [temp retain];            break;        }        temp = [namePart stringByAppendingFormat:@"(%d)",count++];    }    [pool drain];    return temp;}@end

main.m

#import <Foundation/Foundation.h>#import "DirAndFile.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        //存在同名目录名称时,在目录后添加一个数字标记区分同名目录。        NSString *dirPath =@"/Users/mac/work/test-dir(2)";        NSFileManager *fm =[NSFileManager defaultManager];                for (int i =0; i<5; ++i)        {            NSString *tempDir =[dirPath stringByAppendingPathComponent:@"aa"];           tempDir= [DirAndFile getDirPathNewName:tempDir];            [fm createDirectoryAtPath:tempDir withIntermediateDirectories:NO attributes:nil error:nil];            NSLog(@"dir%d:%@",i+1,tempDir);                    }                //存在同名文件名称,文件名后添加数字标识        FILE *file =NULL;        for (int i =0; i<5; ++i)        {            NSString* filePath =[dirPath stringByAppendingPathComponent:@"content.txt"];            filePath =[DirAndFile getFilePathNewName:filePath withPosfix:@".txt"];            file =fopen([filePath UTF8String], "w");            if (file == NULL)            {                break;            }            NSString *content=@"hello world";            fwrite([content UTF8String], strlen([content UTF8String]), 1, file);            fclose(file);                                }            }    return 0;}

运行结果:



0 0
原创粉丝点击