[Object C]_[初级]_[创建文件路径时,已存在同名目录和文件名的问题的解决方案]
2015-11-28 14:45
525 查看
场景:导出数据到本地,以文件夹格式和文件名格式导出,数据中存在同名问题,为了防止不覆盖,要创建一个新的文件夹或者文件名用以区分同名的不同数据。
下面编写一个小例子进行说明:
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;
}
运行结果:
下面编写一个小例子进行说明:
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;
}
运行结果:
相关文章推荐
- NSFileManager和NSFileHandle(附:获取文件大小 )
- [Object C]_[初级]_[NSArray排序]
- [Object C]_[初级]_[两个数组共有元素的提取,删除,数组的合并]
- [Object C]_[初级]_[文件修改创建时间和修改时间]
- Objective-C之启程Objective-C语言介绍
- IOS开发(11)之UITabBarController多视图控制器
- IOS开发(13)之UITextField控件
- IOS开发(12)之UILabel
- IOS开发(14)之UISlider控件
- IOS开发(15)之UITextView控件
- IOS开发(16)之UIButton控件
- IOS开发(17)之UIImageView控件
- 'release' is unavailable: not available in automatic reference counting mode解决方案
- property和synthesize
- @property与内存管理以及可传参数
- id ,NSObject, id<NSObject>区别
- NSFileManager的用法
- iPhone与iPad开发实战——精通Object C--视频
- IPhone之AVAudioRecorder
- 软件开发周期介绍