+ (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. Linux下的/etc/crontab文件和crontab -e命令区别及Crontab命令详解(转)

    /etc/crontab文件和crontab -e命令区别 1.格式不同 前者 # For details see crontabs # Example of job definition: # .- ...

  2. 会话对应的线程id

    http://blog.csdn.net/sqlserverdiscovery/article/details/7968117

  3. shell基本计算、逻辑运算、位运算详解

    转:http://blog.chinaunix.net/uid-8504518-id-3918531.html Shell 提供大量的基本运算操作,在脚本中非常有用.Shell 对您提供的算术表达式求 ...

  4. zip压缩与解压文件夹或文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  5. wpf一些例子

    相关知识点:WPF - Adorner WPF Diagram Designer http://www.codeproject.com/Articles/484616/MVVM-Diagram-Des ...

  6. 防止木马利用iframe框架来调用外域JS代码

    <!--防止木马利用iframe框架来调用外域JS代码,不过滤自己网站的域名的框架网页开始--><SCRIPT LANGUAGE="JavaScript"> ...

  7. centos7使用samba共享文件

    samba是一款可以让linux和windows下共享文件的常用的一款软件 如何在centos7中使用和配置samba 首先先安装 sudo yum install samba 下载完成查看rpm - ...

  8. table表头固定

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  9. elasticsearch term 查询二:Range Query

    Range Query 将文档与具有一定范围内字词的字段进行匹配. Lucene查询的类型取决于字段类型,对于字符串字段,TermRangeQuery,对于数字/日期字段,查询是NumericRang ...

  10. IntelliJ IDEA 控制台中文乱码

    1. 预热 刚刚接触IntelliJ IDEA几天,在易用性方面的确比Eclipse好很多,比较智能,各种插件.工具都已经集成,和Mac OS X类似——开箱即用. 但是还是老大难问题——中文乱码,让 ...