功能列表
//判斷文件是否存在
+ (BOOL)fileExistsAtPath:(NSString *)aPath;
//判斷文件是否存在Documents下
+ (BOOL)fileExistsAtDocumentsWithFileName:(NSString *)afileName;
//判斷文件夾是否存在
+ (BOOL)dirExistAtPath:(NSString *)aPath;
//創(chuàng)建目錄
+ (BOOL)createPath:(NSString *)aPath;
// 創(chuàng)建目錄的上級目錄
+ (BOOL)createParentDirectory:(NSString *)aPath;
// 目錄下創(chuàng)建文件
+ (BOOL)createFileWithPath:(NSString *)aPath content:(NSData *)aContent;
// documents下創(chuàng)建文件
+ (BOOL)createFileAtDocumentsWithName:(NSString *)aFilename content:(NSData *)aContent;
+ (NSString *)createFileWithName:(NSString *)aFilename content:(NSData *)aContent;
//TMP下創(chuàng)建文件
+ (NSString *)createFileAtTmpWithName:(NSString *)aFilename content:(NSData *)aContent;
// Caches下創(chuàng)建文件
+ (BOOL)createFileAtCachesWithName:(NSString *)aFilename content:(NSData *)aContent;
//在Document下創(chuàng)建文件目錄
+ (BOOL)createDirectoryAtDocument:(NSString *)aDirectory;
// 刪除文件
+ (BOOL)deleteFileWithName:(NSString *)aFileName error:(NSError **)aError;
//刪除指定路徑下的文件
+ (BOOL)deleteFileWithUrl:(NSURL *)aUrl error:(NSError **)aError;
//刪除文件夾下的所有文件
+ (BOOL)deleteAllFileAtPath:(NSString *)aPath;
//根據(jù)文件名刪除document下的文件
+ (BOOL)deleteFileAtDocumentsWithName:(NSString *)aFilename error:(NSError **)aError;
//讀取文件
+ (NSData *)readFileWithPath:(NSString *)aPath;
+ (NSData *)readFileWithURL:(NSURL *)aUrl;
+ (NSData *)readFileAtDocumentsWithFileName:(NSString *)aFileName;
//獲取臨時目錄下的所有文件列表
+ (NSArray *)getContentsOfTmpDirectorByTimeOrder;
// 獲取文件大小
+ (unsigned long long)fileSizeAtPaht:(NSString *)aPath;
//遍歷文件夾下的所有文件,不含子文件
+ (NSArray *)getContentsOfDirectoryAtPath:(NSString *)aDirString;
//遍歷文件夾下的所有文件,包含子文件
+ (NSArray *)getAllFilesAtPath:(NSString *)aDirString;
//獲取路徑下通過時間排序的文件列表
+ (NSArray *)getContentsOfDirectoryByTimeOrderAtPath:(NSString *)aDireString;
//復(fù)制一個目錄下的文件到另外一個目錄,前后兩個必須一致汹族,要么都是目錄拖吼,要么都是文件
+ (BOOL) copyItemAtPath:(NSString *)aPath toPath:(NSString *)aDestinationPath error:(NSError **)aError;
//重命名文件
+ (BOOL)renameFileNameFrom:(NSString *)aOldName toPath:(NSString *)aNewName error:(NSError **)aError;
具體實現(xiàn)
static NSFileManager *iFileManager;
+ (NSFileManager *)getNSFileManager{
if (!iFileManager) {
iFileManager = [NSFileManager defaultManager];
}
return iFileManager;
}
#pragma mark - >>>>>>>>> 判斷 <<<<<<<<<<<
#pragma mark - 判斷文件是否存在
+ (BOOL)fileExistsAtPath:(NSString *)aPath{
BOOL result = NO;
if (aPath) {
result = [[self getNSFileManager] fileExistsAtPath:aPath];
}
return result;
}
#pragma mark - 判斷文件是否存在Documents下
+ (BOOL)fileExistsAtDocumentsWithFileName:(NSString *)afileName{
BOOL result = NO;
if (afileName) {
NSString *fullFileNamePath = [self getFullDocumentPathWithName:afileName];
result = [[self getNSFileManager] fileExistsAtPath:fullFileNamePath];
}
return result;
}
#pragma mark - 判斷文件夾是否存在
+ (BOOL)dirExistAtPath:(NSString *)aPath
{
BOOL isDir = NO;
BOOL result = [[self getNSFileManager] fileExistsAtPath:aPath isDirectory:&isDir];
return result && isDir;
}
#pragma mark - >>>>>>>>>創(chuàng)建<<<<<<<<<<<
#pragma mark - 創(chuàng)建目錄
+ (BOOL)createPath:(NSString *)aPath{
BOOL result = NO;
result =[self createParentDirectory:aPath];
if (result) {
result = [ [self getNSFileManager] createDirectoryAtPath:aPath withIntermediateDirectories:YES attributes:nil error:nil];
}
return result;
}
#pragma mark - 創(chuàng)建目錄的上級目錄
+ (BOOL)createParentDirectory:(NSString *)aPath
{
//存在上級目錄,并且上級目錄不存在的創(chuàng)建所有的上級目錄
BOOL result = NO;
NSString *parentPath = [self getParentPath:aPath];
if (parentPath && ![self dirExistAtPath:parentPath]) {
return [[self getNSFileManager] createDirectoryAtPath:parentPath withIntermediateDirectories:YES attributes:nil error:nil];
}
else if ([self dirExistAtPath:parentPath]){
result = YES;
}
return result;
}
#pragma mark 目錄下創(chuàng)建文件
+ (BOOL)createFileWithPath:(NSString *)aPath content:(NSData *)aContent
{
BOOL result = NO;
result = [self createParentDirectory:aPath];
if (result) {
result = [[self getNSFileManager] createFileAtPath:aPath contents:aContent attributes:nil];
}
return result;
}
#pragma mark documents下創(chuàng)建文件
+ (BOOL)createFileAtDocumentsWithName:(NSString *)aFilename
content:(NSData *)aContent
{
NSString *filePath =[self getFullDocumentPathWithName:aFilename];
BOOL result = [self createFileWithPath:filePath
content:aContent];
return result;
}
+ (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 TMP下創(chuàng)建文件
+ (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;
}
#pragma mark Caches下創(chuàng)建文件
+ (BOOL)createFileAtCachesWithName:(NSString *)aFilename
content:(NSData *)aContent
{
NSString *filePath =[self getFullCachesPathWithName:aFilename];
BOOL result = [self createFileWithPath:filePath
content:aContent];
return result;
}
#pragma mark 在Document下創(chuàng)建文件目錄
+ (BOOL)createDirectoryAtDocument:(NSString *)aDirectory
{
NSString * directoryAll = [self getFullDocumentPathWithName:aDirectory];
BOOL result = [ [self getNSFileManager] createDirectoryAtPath:directoryAll
withIntermediateDirectories:YES
attributes:nil
error:nil];
return result;
}
#pragma mark - >>>>>>>>>刪除<<<<<<<<<
#pragma mark 刪除文件
+ (BOOL)deleteFileWithName:(NSString *)aFileName
error:(NSError **)aError
{
NSFileManager *tempFileManager = [self getNSFileManager];
return [tempFileManager removeItemAtPath:aFileName
error:aError];
}
#pragma mark - 刪除指定路徑下的文件
+ (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;
for (int i = 0; i<[fileArray count]; i++)
{
filePath = [aPath stringByAppendingPathComponent:[fileArray objectAtIndex:i]];
result = [[self getNSFileManager] removeItemAtPath:filePath
error:nil];
if (!result)
{
break;
}
filePath = nil;
}
return result;
}
#pragma mark 根據(jù)文件名刪除document下的文件
+ (BOOL)deleteFileAtDocumentsWithName:(NSString *)aFilename
error:(NSError **)aError
{
NSString *filePath = [self getFullDocumentPathWithName:aFilename];
return [self deleteFileWithName:filePath
error:aError];
}
#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];
NSData *data = [NSData dataWithContentsOfFile:fullPathWithName];
return data;
}
#pragma mark - 獲取臨時目錄下的所有文件列表
+ (NSArray *)getContentsOfTmpDirectorByTimeOrder
{
return [self getContentsOfDirectoryByTimeOrderAtPath:[self getTmpPath]];
}
#pragma mark - 獲取文件大小
+ (unsigned long long)fileSizeAtPaht:(NSString *)aPath
{
return [[[self getNSFileManager] attributesOfItemAtPath:aPath error:nil] fileSize];
}
#pragma mark - >>>>>>>>>獲取<<<<<<<<<<<
#pragma mark - 獲取上級目錄
+ (NSString *)getParentPath:(NSString *)aPath
{
// //刪除最后一個目錄
return [aPath stringByDeletingLastPathComponent];
}
#pragma mark 根據(jù)文件名稱獲取documents的文件名的全路徑
+ (NSString *)getFullDocumentPathWithName:(NSString *)aFileName
{
return [[self getDocumentPath] stringByAppendingString:aFileName];
}
#pragma mark 根據(jù)文件名稱獲取tmp的文件名的全路徑
+ (NSString *)getFullTmpPathWithName:(NSString *)aFileName
{
return [[self getTmpPath] stringByAppendingPathComponent:aFileName];
}
#pragma mark 根據(jù)文件名稱獲取Caches的文件名的全路徑
+ (NSString *)getFullCachesPathWithName:(NSString *)aFileName
{
return [[self getCachesPath] stringByAppendingPathComponent:aFileName];
}
+ (NSString *)getHomePath
{
NSString *home = [@"~" stringByExpandingTildeInPath];
return home;
}
#pragma mark 獲取documents的全路徑
+ (NSString *)getDocumentPath
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *result = [path objectAtIndex:0];
return result;
}
#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:0];
}
#pragma mark 遍歷文件夾下的所有文件,不含子文件
+ (NSArray *)getContentsOfDirectoryAtPath:(NSString *)aDirString
{
return [[self getNSFileManager] contentsOfDirectoryAtPath:aDirString
error:nil];
}
#pragma mark - 獲取路徑下通過時間排序的文件列表
+ (NSArray *)getContentsOfDirectoryByTimeOrderAtPath:(NSString *)aDireString
{
NSArray *files = [self getAllFilesAtPath:(NSString *)aDireString];
NSMutableArray *iUrls = [[NSMutableArray alloc] initWithCapacity:1];
NSArray *sortedFiles = nil;
if([files count] > 0)
{
sortedFiles = [files sortedArrayUsingComparator:^(NSString *url1, NSString *url2)
{
NSDictionary *fileAttributes1 = [[self getNSFileManager] attributesOfItemAtPath:url1
error:nil];
NSDictionary *fileAttributes2 = [[self getNSFileManager] attributesOfItemAtPath:url2
error:nil];
NSDate *date1 = [fileAttributes1 objectForKey:NSFileCreationDate] ;
NSDate *date2 = [fileAttributes2 objectForKey:NSFileCreationDate] ;
return [date2 compare:date1];
}];
}
for (int i = 0; i < [sortedFiles count]; i++)
{
NSURL *url = [NSURL fileURLWithPath:[sortedFiles objectAtIndex:i]];
[iUrls addObject:url];
}
return iUrls;
}
#pragma mark - 遍歷文件夾下的所有文件,包含子文件
+ (NSArray *)getAllFilesAtPath:(NSString *)aDirString
{
NSMutableArray *tempPathArray = [NSMutableArray array];
NSArray *tempArray =[self getContentsOfDirectoryAtPath:aDirString];
NSString *fullPath = nil;
for (NSString *fileNmae in tempArray) {
BOOL flag = YES;
fullPath = [aDirString stringByAppendingPathComponent:fileNmae];
//判斷是否存在
if ([[self getNSFileManager] fileExistsAtPath:fullPath isDirectory:&flag]) {
//不是目錄,直接添加
if (![[fileNmae substringToIndex:1]isEqualToString:@"."]) {
[tempPathArray addObject:fullPath];
}
//如果是目錄,一當前文件夾為key,文件夾下的子文件名為value,遞歸調(diào)用
else{
NSArray *subPathArray =[self getAllFilesAtPath:fullPath];
[tempPathArray addObjectsFromArray:subPathArray];
}
}
fullPath = nil;
}
NSArray *resultArray = [NSArray arrayWithArray:tempPathArray];
return resultArray;
}
#pragma mark 復(fù)制一個目錄下的文件到另外一個目錄,前后兩個必須一致晒奕,要么都是目錄开瞭,要么都是文件
+ (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;
}
@end