原文出處 http://blog.csdn.net/feng2qing/article/details/54974200
//獲取Document路徑
+ (NSString*)getDocumentPath
{NSArray*filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);return[filePaths objectAtIndex:0];
}
//獲取Library路徑
+ (NSString*)getLibraryPath
{NSArray*filePaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask,YES);return[filePaths objectAtIndex:0];
}
//獲取應(yīng)用程序路徑+ (NSString*)getApplicationPath
{returnNSHomeDirectory();
}
//獲取Cache路徑
+ (NSString*)getCachePath
{NSArray*filePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES);return[filePaths objectAtIndex:0];
}
//獲取Temp路徑+ (NSString*)getTempPath
{returnNSTemporaryDirectory();
}
//判斷文件是否存在于某個(gè)路徑中
+ (BOOL)fileIsExistOfPath:(NSString*)filePath
{BOOLflag =NO;NSFileManager*fileManager = [NSFileManagerdefaultManager];if([fileManager fileExistsAtPath:filePath]) {
flag =YES;
}else{
flag =NO;
}returnflag;
}
//從某個(gè)路徑中移除文件
+ (BOOL)removeFileOfPath:(NSString*)filePath
{BOOLflag =YES;NSFileManager*fileManage = [NSFileManagerdefaultManager];if([fileManage fileExistsAtPath:filePath]) {if(![fileManage removeItemAtPath:filePath error:nil]) {
flag =NO;
}
}returnflag;
}
//從URL路徑中移除文件
- (BOOL)removeFileOfURL:(NSURL*)fileURL
{BOOLflag =YES;NSFileManager*fileManage = [NSFileManagerdefaultManager];if([fileManage fileExistsAtPath:fileURL.path]) {if(![fileManage removeItemAtURL:fileURL error:nil]) {
flag =NO;
}
}returnflag;
}
//創(chuàng)建文件路徑
+(BOOL)creatDirectoryWithPath:(NSString*)dirPath
{BOOLret =YES;BOOLisExist = [[NSFileManagerdefaultManager] fileExistsAtPath:dirPath];if(!isExist) {NSError*error;BOOLisSuccess = [[NSFileManagerdefaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YESattributes:nilerror:&error];if(!isSuccess) {
ret =NO;NSLog(@"creat Directory Failed. errorInfo:%@",error);
}
}returnret;
}
//創(chuàng)建文件
+ (BOOL)creatFileWithPath:(NSString*)filePath
{BOOLisSuccess =YES;NSFileManager*fileManager = [NSFileManagerdefaultManager];BOOLtemp = [fileManager fileExistsAtPath:filePath];if(temp) {returnYES;
}NSError*error;//stringByDeletingLastPathComponent:刪除最后一個(gè)路徑節(jié)點(diǎn)NSString*dirPath = [filePath stringByDeletingLastPathComponent];
isSuccess = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YESattributes:nilerror:&error];if(error) {NSLog(@"creat File Failed. errorInfo:%@",error);
}if(!isSuccess) {returnisSuccess;
}
isSuccess = [fileManager createFileAtPath:filePath contents:nilattributes:nil];returnisSuccess;
}
//保存文件
+ (BOOL)saveFile:(NSString*)filePath withData:(NSData *)data
{BOOLret =YES;
ret = [selfcreatFileWithPath:filePath];if(ret) {
ret = [data writeToFile:filePath atomically:YES];if(!ret) {NSLog(@"%s Failed",__FUNCTION__);
}
}else{NSLog(@"%s Failed",__FUNCTION__);
}returnret;
}//追加寫文件+ (BOOL)appendData:(NSData *)data withPath:(NSString*)path
{BOOLresult = [selfcreatFileWithPath:path];if(result) {
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:path];
[handle seekToEndOfFile];
[handle writeData:data];
[handle synchronizeFile];
[handle closeFile];returnYES;
}else{NSLog(@"%s Failed",__FUNCTION__);returnNO;
}
}
//獲取文件
+ (NSData *)getFileData:(NSString*)filePath
{
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *fileData = [handle readDataToEndOfFile];
[handle closeFile];returnfileData;
}
//讀取文件
+ (NSData *)getFileData:(NSString*)filePath startIndex:(longlong)startIndex length:(NSInteger)length
{
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
[handle seekToFileOffset:startIndex];
NSData *data = [handle readDataOfLength:length];
[handle closeFile];returndata;
}
//移動(dòng)文件
+ (BOOL)moveFileFromPath:(NSString*)fromPath toPath:(NSString*)toPath
{NSFileManager*fileManager = [NSFileManagerdefaultManager];if(![fileManager fileExistsAtPath:fromPath]) {NSLog(@"Error: fromPath Not Exist");returnNO;
}if(![fileManager fileExistsAtPath:toPath]) {NSLog(@"Error: toPath Not Exist");returnNO;
}NSString*headerComponent = [toPath stringByDeletingLastPathComponent];if([selfcreatFileWithPath:headerComponent]) {return[fileManager moveItemAtPath:fromPath toPath:toPath error:nil];
}else{returnNO;
}
}
//拷貝文件
+(BOOL)copyFileFromPath:(NSString*)fromPath toPath:(NSString*)toPath
{NSFileManager*fileManager = [NSFileManagerdefaultManager];if(![fileManager fileExistsAtPath:fromPath]) {NSLog(@"Error: fromPath Not Exist");returnNO;
}if(![fileManager fileExistsAtPath:toPath]) {NSLog(@"Error: toPath Not Exist");returnNO;
}NSString*headerComponent = [toPath stringByDeletingLastPathComponent];if([selfcreatFileWithPath:headerComponent]) {return[fileManager copyItemAtPath:fromPath toPath:toPath error:nil];
}else{returnNO;
}
}
//獲取文件夾下文件列表
+ (NSArray*)getFileListInFolderWithPath:(NSString*)path
{NSFileManager*fileManager = [NSFileManagerdefaultManager];NSError*error;NSArray*fileList = [fileManager contentsOfDirectoryAtPath:path error:&error];if(error) {NSLog(@"getFileListInFolderWithPathFailed, errorInfo:%@",error);
}returnfileList;
}
//獲取文件大小
+ (longlong)getFileSizeWithPath:(NSString*)path
{
unsignedlonglongfileLength =0;
NSNumber*fileSize;
NSFileManager*fileManager = [NSFileManagerdefaultManager];NSDictionary*fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
if((fileSize = [fileAttributes objectForKey:NSFileSize])) {
fileLength = [fileSize unsignedLongLongValue];
return fileLength;
}
//
NSFileManager* manager =[NSFileManager defaultManager];
//? ?
?if ([manager fileExistsAtPath:path]){
//? ? ? ??
return [[manager attributesOfItemAtPath:path error:nil] fileSize];
//??
? }
//
? ? return 0;
}
//獲取文件創(chuàng)建時(shí)間
+ (NSString*)getFileCreatDateWithPath:(NSString*)path
{NSString*date =nil;NSFileManager*fileManager = [NSFileManagerdefaultManager];NSDictionary*fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
date = [fileAttributes objectForKey:NSFileCreationDate];returndate;
}
//獲取文件所有者
+ (NSString*)getFileOwnerWithPath:(NSString*)path
{NSString*fileOwner =nil;NSFileManager*fileManager = [NSFileManagerdefaultManager];NSDictionary*fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
fileOwner = [fileAttributes objectForKey:NSFileOwnerAccountName];returnfileOwner;
}
//獲取文件更改日期
+ (NSString*)getFileChangeDateWithPath:(NSString*)path
{NSString*date =nil;NSFileManager*fileManager = [NSFileManagerdefaultManager];NSDictionary*fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
date = [fileAttributes objectForKey:NSFileModificationDate];returndate;
}