[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
- [Object C]_[初级]_[创建文件路径时,已存在同名目录和文件名的问题的解决方案]
- [Object C]_[初级]_[文件修改创建时间和修改时间]
- [Object-C]_[初级]_[object类的对象和属性@property]
- [Object c]_[初级]_[NSString常用方法的总结]
- [C/C++标准库]_[初级]_[读写中文路径的文件--写入unicode字符串]
- [C/C++标准库]_[初级]_[获取文件的所在目录跨平台实现]
- 出现 “未能创建此平台,因为已存在同名的解决方案平台”提示
- [C++/object c]_[初级]_[std::map容器的使用总结和NSDictionary词典使用总结]
- [Cocoa]_[初级]_[vcf文件的生成和导出]
- [Object C]_[初级]_[NSArray排序]
- [Object C]_[初级]_[两个数组共有元素的提取,删除,数组的合并]
- [Object-c]_[初级]_[数组NSArray过滤NSPredicate的简单介绍]
- Mint 回收站下恢复不同目录下的同名文件时存在其中之一文件添加后缀的问题的解决
- [Object C]_[初级]_[Object C之内存管理]
- 给一个目录下的一个已存在的文件生成一个新的文件名
- 比较不同目录文件夹下同名文件的数量和显示同名文件名
- C++/C语言_宏函数存在的问题
- [Windows]_[初级]_[修改文件的创建时间-修改时间-访问时间]
- 文法与语言分类
- 在布局文件里添加toolbar时出现的错误error: Error parsing XML: unbound prefix
- 一键退出
- LeetCode94——Binary Tree Inorder Traversal
- 自定义带删除图标的EditText
- [Object C]_[初级]_[创建文件路径时,已存在同名目录和文件名的问题的解决方案]
- 公式
- C++primer plus第六版课后编程练习答案8.2
- rapid7大规模扫描项目
- 设置系统自带UIBarButtonItem
- Win7 中安装Theano及配置CUDA以搭建GPU加速环境
- Myeclipse2014黑色主题风格设置
- 关于网站嵌入【视频播放器】的规则和例子
- 纯虚函数 http://blog.chinaunix.net/uid-20585352-id-62720.html