//
// LGFileManager.h// FileNameDemo
//
// Created by li.gang on 14-4-23.
// Copyright (c) 2014年 LG. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface LGFileManager : NSObject
//返回数据库在沙盒中的路径
+ (NSString *)getPath;
//传入文件a.png,如果documents里面有此文件名 那就自动生成a(1).png a(2).png a(3).png......
+ (NSString *)getSaveName:(NSString *)name;
+ (void)saveVideoData:(NSData *)data filePath:(NSString *)urlString;
@end
//
// LGFileManager.m
// FileNameDemo
//
// Created by li.gang on 14-4-23.
// Copyright (c) 2014年 LG. All rights reserved.
//
#import "LGFileManager.h"
@implementation LGFileManager
//返回数据库在沙盒中的路径
+ (NSString *)getPath
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
, NSUserDomainMask
, YES);
NSString *path = [paths objectAtIndex:0];
return path;
}
//传入文件a.png,如果documents/tmp里面有此文件名 那就自动生成a(1).png a(2).png a(3).png......
+ (NSString *)getSaveName:(NSString *)name{
NSString *path = [self getPath];
NSFileManager * fileManager = [NSFileManager defaultManager];
NSArray * tempFileList = [[NSArray alloc] initWithArray:[fileManager contentsOfDirectoryAtPath:path error:nil]];
NSString *Qname = [[name componentsSeparatedByString:@"."] objectAtIndex:0];
//拿到同名的文件
NSMutableArray *mutArrName = [NSMutableArray array];
for (NSString *s in tempFileList) {
if ([s rangeOfString:Qname].location !=NSNotFound){
[mutArrName addObject:s];
}
}
//如果没有同名的直接 直接返回name a.png
if (mutArrName.count == 0) return name;
if (mutArrName.count == 1) {
//如果有一个文件 还需要判断是不是a.png 是就返回a(1).png
if ([name isEqualToString:[mutArrName objectAtIndex:0]]) {
return [NSString stringWithFormat:@"%@(1).png",[[name componentsSeparatedByString:@"."] objectAtIndex:0]];//返回a(1).png
}else{
return name;//返回a.png
}
}else{
int k = 0;//有多个文件 首先判断有没有a.png k=0是没有 k=1是有
NSMutableArray *mutArrNum = [NSMutableArray array];//存放增加的数字字符串
for (NSString *s in mutArrName) {
if ([s isEqualToString:name]) {
k = 1;
}
if ([s rangeOfString:@"("].location !=NSNotFound) {
NSString *num = [[[[s componentsSeparatedByString:@"("] objectAtIndex:1] componentsSeparatedByString:@")"] objectAtIndex:0];
[mutArrNum addObject:num];
}
}
if (k == 0) {
return name;//返回a.png
}
for (int i=1; i <= mutArrNum.count+1; i++) {
int j = 0;
for (NSString *s in mutArrNum) {
if (i == [s integerValue]) {
j = 1;
}
}
if (j == 0) {
return [NSString stringWithFormat:@"%@(%d).png",[[name componentsSeparatedByString:@"."] objectAtIndex:0],i];//返回a(i).png
}
}
}
return name;
}
+ (void)saveVideoData:(NSData *)data filePath:(NSString *)urlString
{
NSString * getName = [self getSaveName:urlString];
//初始化Documents路径
NSString *path = [[self getPath]
stringByAppendingPathComponent:@""];
NSString *name = [NSString stringWithFormat:@"/%@",getName];
//初始化文件路径
NSString *fileName=[path stringByAppendingPathComponent:name];
//判断temp文件夹是否存在
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL fileExists = [fileManager fileExistsAtPath:path];
if (!fileExists) {//如果不存在说创建,因为下载时,不会自动创建文件夹
[fileManager createDirectoryAtPath:path
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
if ([data writeToFile:fileName atomically:YES]) {
NSLog(@">>write ok.");
}
}
@end
Demo下载地址:http://download.csdn.net/download/conslee/7237547