+ (NSFileManager *)getNSFileManager
{
// iNSFileManager是一个静态变量
if (!iNSFileManager)
{
iNSFileManager = [NSFileManager defaultManager];
}
return iNSFileManager;
} #pragma mark 判断文件是否存在 + (BOOL)fileExistsAtPath:(NSString *)aPath
{
BOOL result = NO;
if (aPath)
{
result = [[self getNSFileManager] fileExistsAtPath:aPath]; }
return result;
} + (BOOL)fileExistsAtDocumentsWithFileName:(NSString *)aFileName{
BOOL result = NO;
if (aFileName)
{
NSString *fullFileName = [self getFullDocumentPathWithName:aFileName];
WALog(fullFileName); result = [[self getNSFileManager] fileExistsAtPath:fullFileName];
}
return result;
} #pragma mark 判断文件夹是否存在
+ (BOOL)dirExistsAtPath:(NSString *)aPath
{
BOOL isDir = NO;
BOOL result = [[self getNSFileManager] fileExistsAtPath:aPath
isDirectory:&isDir];
return result && isDir;
} #pragma mark 获取上级目录
+ (NSString *) getParentPath:(NSString *)aPath
{
return [aPath stringByDeletingLastPathComponent]; } #pragma mark 创建目录的上级目录
+ (BOOL)createParentDirectory:(NSString *)aPath
{
//存在上级目录,并且上级目录不存在的创建所有的上级目录
BOOL result = NO;
NSString *parentPath = [self getParentPath:aPath];
if (parentPath && ![self dirExistsAtPath:parentPath])
{
result = [[self getNSFileManager] createDirectoryAtPath:parentPath
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
else if ([self dirExistsAtPath:parentPath]){
result = YES;
}
return result;
} #pragma mark 创建目录
+ (BOOL)createPath:(NSString *)aPath
{
NSFileManager *tempFileManager = [self getNSFileManager];
BOOL result = NO;
result = [self createParentDirectory:aPath];
if (result)
{
result = [tempFileManager createDirectoryAtPath:aPath
withIntermediateDirectories:YES
attributes:nil
error:nil]; }
return result;
} #pragma mark 目录下创建文件
+ (BOOL)createFileWithPath:(NSString *)aPath content:(NSData *)aContent
{
NSFileManager *tempFileManager = [self getNSFileManager];
BOOL result = NO;
result = [self createParentDirectory:aPath];
if (result)
{
result = [tempFileManager createFileAtPath:aPath
contents:aContent
attributes:nil];
}
return result;
} #pragma mark documents下创建文件
+ (BOOL)createFileAtDocumentsWithName:(NSString *)aFilename
content:(NSData *)aContent
{
NSString *filePath =[self getFullDocumentPathWithName:aFilename];
BOOL result = [self createFileWithPath:filePath
content:aContent];
return result;
} + (NSString *)createFileAtTmpWithName:(NSString *)aFilename
content:(NSData *)aContent
{
NSString *filePath =[self getFullTmpPathWithName:aFilename];
BOOL result = [self createFileWithPath:filePath
content:aContent];
if(!result)
{
filePath = nil;
}
return filePath; } + (NSString *)createFileWithName:(NSString *)aFilename
content:(NSData *)aContent
{
NSString *filePath =[self getFullDocumentPathWithName:aFilename];
BOOL result = [self createFileWithPath:filePath
content:aContent];
if(!result)
{
filePath = nil;
}
return filePath;
} #pragma mark Caches下创建文件
+ (BOOL)createFileAtCachesWithName:(NSString *)aFilename
content:(NSData *)aContent
{
NSString *filePath =[self getFullCachesPathWithName:aFilename];
BOOL result = [self createFileWithPath:filePath
content:aContent];
return result;
}
#pragma mark 根据文件名称获取Caches的文件名的全路径,需要自己释放
+ (NSString *)getFullCachesPathWithName:(NSString *)aFileName
{
return [[self getCachesPath] stringByAppendingPathComponent:aFileName];
} + (NSString *)addSubPath:(NSString *)aSubPath
toPath:(NSString *)aPath
{
return [aPath stringByAppendingPathComponent:aSubPath];
} #pragma mark 根据文件名称获取documents的文件名的全路径,需要自己释放
+ (NSString *)getFullDocumentPathWithName:(NSString *)aFileName
{
return [[self getDocumentPath] stringByAppendingPathComponent:aFileName];
} #pragma mark 根据文件名称获取tmp的文件名的全路径,需要自己释放
+ (NSString *)getFullTmpPathWithName:(NSString *)aFileName
{
return [[self getTmpPath] stringByAppendingPathComponent:aFileName];
} #pragma mark 获取documents的全路径
+ (NSString *)getDocumentPath
{
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *result = [pathArray objectAtIndex:];
return result; } + (NSString *)getHomePath
{
NSString *home = [@"~" stringByExpandingTildeInPath];
return home;
} #pragma mark 删除文件
+ (BOOL)deleteFileWithName:(NSString *)aFileName
error:(NSError **)aError
{
NSFileManager *tempFileManager = [self getNSFileManager];
return [tempFileManager removeItemAtPath:aFileName
error:aError];
} + (BOOL)deleteFileWithUrl:(NSURL *)aUrl error:(NSError **)aError
{
return [[self getNSFileManager] removeItemAtURL:aUrl error:aError];
} #pragma mark 删除文件夹下的所有文件
+ (BOOL)deleteAllFileAtPath:(NSString *)aPath
{
BOOL result = NO;
NSArray *fileArray = [self getContentsOfDirectoryAtPath:aPath]; NSString *filePath = nil; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (int i = ; i<[fileArray count]; i++)
{
filePath = [aPath stringByAppendingPathComponent:[fileArray objectAtIndex:i]];
result = [[self getNSFileManager] removeItemAtPath:filePath
error:nil];
if (!result)
{
break;
}
filePath = nil;
}
[pool drain];
return result;
} #pragma mark 根据文件名删除document下的文件
+ (BOOL)deleteFileAtDocumentsWithName:(NSString *)aFilename
error:(NSError **)aError
{
NSString *filePath = [self getFullDocumentPathWithName:aFilename];
return [self deleteFileWithName:filePath
error:aError];
} #pragma mark 获取tmp路径
+ (NSString *)getTmpPath
{
NSString *pathName = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
return pathName;
} #pragma mark 获取caches路径
+ (NSString *)getCachesPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
return [paths objectAtIndex:];
} #pragma mark 在Document下创建文件目录
+ (BOOL)createDirectoryAtDocument:(NSString *)aDirectory
{
NSFileManager *tempFileManager = [self getNSFileManager];
NSString * directoryAll = [self getFullDocumentPathWithName:aDirectory]; BOOL result = [tempFileManager createDirectoryAtPath:directoryAll
withIntermediateDirectories:YES
attributes:nil
error:nil];
return result;
} #pragma mark 读取文件
+ (NSData *)readFileWithPath:(NSString *)aPath
{
NSData *data = [NSData dataWithContentsOfFile:aPath];
return data;
} + (NSData *)readFileWithURL:(NSURL *)aUrl
{
NSData *data = [NSData dataWithContentsOfURL:aUrl];
return data;
}
+ (NSData *)readFileAtDocumentsWithFileName:(NSString *)aFileName
{
NSString *fullPathWithName = [self getFullDocumentPathWithName:aFileName];
WALog(fullPathWithName);
NSData *data = [NSData dataWithContentsOfFile:fullPathWithName];
return data;
} #pragma mark 遍历文件夹下的所有文件,不含子文件
+ (NSArray *)getContentsOfDirectoryAtPath:(NSString *)aDirString {
NSFileManager *tempFileManager = [self getNSFileManager];
return [tempFileManager contentsOfDirectoryAtPath:aDirString
error:nil];
} + (NSArray *)getContentsOfDirectoryByTimeOrderAtPath:(NSString *)aDireString
{
NSArray *files = [CWAFileUtil getAllFilesAtPath:(NSString *)aDireString]; NSMutableArray *iUrls = [[NSMutableArray alloc] initWithCapacity:];
NSArray *sortedFiles = nil; if([files count] > )
{
sortedFiles = [files sortedArrayUsingComparator:^(NSString *url1, NSString *url2)
{ NSDictionary *fileAttributes1 = [[CWAFileUtil getNSFileManager] attributesOfItemAtPath:url1
error:nil]; NSDictionary *fileAttributes2 = [[CWAFileUtil getNSFileManager] attributesOfItemAtPath:url2
error:nil];
NSDate *date1 = [fileAttributes1 objectForKey:NSFileCreationDate] ; NSDate *date2 = [fileAttributes2 objectForKey:NSFileCreationDate] ;
return [date2 compare:date1];
}];
} for (int i = ; i < [sortedFiles count]; i++)
{
NSURL *url = [NSURL fileURLWithPath:[sortedFiles objectAtIndex:i]];
[iUrls addObject:url];
} return [iUrls autorelease]; } + (NSArray *)getContentsOfTmpDirectorByTimeOrder
{
return [self getContentsOfDirectoryByTimeOrderAtPath:[self getTmpPath]];
} + (unsigned long long)fileSizeAtPaht:(NSString *)aPath
{
return [[[self getNSFileManager] attributesOfItemAtPath:aPath error:nil] fileSize];
} #pragma mark 遍历文件夹下的所有文件,含子文件
+ (NSArray *)getAllFilesAtPath:(NSString *)aDirString
{
NSMutableArray *temPathArray = [NSMutableArray array]; NSFileManager *tempFileManager = [self getNSFileManager];
NSArray *tempArray = [self getContentsOfDirectoryAtPath:aDirString];
NSString *fullPath = nil; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (NSString *fileName in tempArray)
{ BOOL flag = YES;
fullPath = [aDirString stringByAppendingPathComponent:fileName]; //判断是否存在
if ([tempFileManager fileExistsAtPath:fullPath
isDirectory:&flag])
{
//不是目录,直接添加
if (!flag)
{
// ignore .DS_Store
if (![[fileName substringToIndex:] isEqualToString:@"."])
{
[temPathArray addObject:fullPath];
}
}
//如果是目录的话,以当前文件夹为key,文件夹下的子文件名为value,递归调用
else
{
NSArray *subPathArray = [self getAllFilesAtPath:fullPath];
// NSDictionary *subPathDic = [[NSDictionary alloc] initWithObjectsAndKeys:subPathArray,fullPath,nil];
[temPathArray addObjectsFromArray:subPathArray];
// [subPathDic release];
}
}
fullPath = nil; }
[pool drain];
NSArray *resultArray = [NSArray arrayWithArray:temPathArray]; return resultArray; } #pragma mark 复制一个目录下的文件到另外一个目录,前后两个必须一致,要么都是目录,要么都是文件
+ (BOOL) copyItemAtPath:(NSString *)aPath
toPath:(NSString *)aDestinationPath
error:(NSError **)aError
{
NSFileManager *tempFileManager = [self getNSFileManager];
return [tempFileManager copyItemAtPath:aPath
toPath:aDestinationPath
error:aError];
} #pragma mark 重命名文件
+ (BOOL)renameFileNameFrom:(NSString *)aOldName
toPath:(NSString *)aNewName
error:(NSError **)aError{
NSFileManager *tempFileManager = [self getNSFileManager];
BOOL result = [tempFileManager moveItemAtPath:aOldName
toPath:aNewName
error:aError];
return result;
}

IOS项目开发中的文件和文件夹操作的更多相关文章

  1. iOS项目开发中的知识点与问题收集整理①(Part 一)

    前言部分 注:本文并非绝对原创 大部分内容摘自 http://blog.csdn.net/hengshujiyi/article/details/20943045 文中有些方法可能已过时并不适用于现在 ...

  2. iOS项目开发中的知识点与问题收集整理①

    前言部分 注:本文并非绝对原创 大部分内容摘自 http://blog.csdn.net/hengshujiyi/article/details/20943045 文中有些方法可能已过时并不适用于现在 ...

  3. iOS项目开发中的知识点与问题收集整理②(Part 二)

    1.点击UIButton 无法产生触摸事件    如果在UIImageView中添加了一个按钮,你会发现在默认情况下这个按钮是无法被点击的,需要设置UIImageView的userInteractio ...

  4. iOS项目开发中的知识点与问题收集整理②

    1.点击UIButton 无法产生触摸事件    如果在UIImageView中添加了一个按钮,你会发现在默认情况下这个按钮是无法被点击的,需要设置UIImageView的userInteractio ...

  5. iOS项目开发中的目录结构

    目录结构: 1.AppDelegate   这个目录下放的是AppDelegate.h(.m)文件,是整个应用的入口文件,所以单独拿出来.   2.Models   这个目录下放一些与数据相关的Mod ...

  6. iOS项目开发日常之创建文件(协议、类、分类、扩展)

    iOS项目开发过程中,是以不断创建文件的形式进行着的. 创建得比较频繁的文件类型是: 这两个类型中创建的文件有:子类.分类.扩展.协议四种文件,如下:    这四类文件是频繁创建的,我们来看一下各自分 ...

  7. ios项目里扒出来的json文件

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...

  8. Android项目开发填坑记-so文件引发的攻坚战

    故事的最初 我负责的项目A要求有播放在线视频的功能,当时从别人的聊天记录的一瞥中发现百度有相关的SDK,当时找到的是Baidu-T5Player-SDK-Android-1.4s,项目中Demo的so ...

  9. iOS项目开发实战——学会使用TableView列表控件(四)plist读取与Section显示

    文本将会实现把数据存储到plist文件里.然后在程序中进行读取.在TableView控件中依据不同的类别显示Section. 有关TableView 的其它实现,请參考<iOS项目开发实战--学 ...

随机推荐

  1. binlog监听工具-canal

    官网 https://github.com/alibaba/canal/wiki

  2. CentOS 6.9下的Setup工具(用于管理服务/防火墙/网络配置/验证服务)

    说明:Setup工具套件好像是CentOS下特有的用于管理服务/防火墙/网络配置等,其实就是基于命令行模式界面的GUI工具.唯一特点就是方便. 安装: #安装Setup命令工具 yum -y inst ...

  3. 《新一代视频压缩码标准-H.264_AVC》读书笔记1

    摘要 第一章 绪论 正文 1.一般而言,视频信号信息量大,传输网络所需要的带宽相对较宽.例如,一路可视电话或会议电视信号,由于其活动内容较少,所需带宽较窄,但要达到良好质量,不压缩约需若干 Mbps, ...

  4. win8.1无法安装安装.net framework 3.5 解决办法【转】

    安装流程1.以系统管理员开启命令提示符(命令提示字符)2挂载windows8.1异3,在命令提示符下输入Dism /online /enablefeature/featurename:NetFx3 / ...

  5. http://zhidao.baidu.com/link?url=3tJ_i5gyYLrd7rFPk0eRYre_oxjCZvTOMOutp89LGhUgi6Ic6Ncama_GMAHnwfF73SVYGqy364vDfv6AY4ERPa

    http://zhidao.baidu.com/link?url=3tJ_i5gyYLrd7rFPk0eRYre_oxjCZvTOMOutp89LGhUgi6Ic6Ncama_GMAHnwfF73SV ...

  6. 【前后台分离模式下,使用OAuth Token方式认证】

    AngularJS is an awesome javascript framework. With it’s $resource service it is super fast and easy ...

  7. css - border-radius

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 配置 mybatis的 log4j.properties

    log4j.rootLogger=debug,stdout,logfile ### 把日志信息输出到控制台 ### log4j.appender.stdout=org.apache.log4j.Con ...

  9. BZOJ 1016 JSOI 2008 最小生成树计数 Kruskal+搜索

    题目大意:给出一些边,求出一共能形成多少个最小生成树. 思路:最小生成树有非常多定理啊,我也不是非常明确.这里仅仅简单讲讲做法.关于定各种定理请看这里:http://blog.csdn.net/wyf ...

  10. 【BIEE】12_查看BIEE的物理SQL

    有时候,我们在使用BIEE的时候回出现一些问题,需要借助物理SQL来进行问题分析.通过物理SQL我们就可以看到BIEE在数据库中是如何去检索出数据库. 查看物理SQL的方式 [登录BIEE]--[管理 ...