想操做文件,該去了解下NSFileManager數組
注意://小竅門:打印數組或者字典,裏面包含中文,直接用%@打印會看不到中文,可用for遍歷訪問code
NSFileManager *fileManager = [NSFileManager defaultManager];
#define LogBool(value) NSLog(@"%@",value==YES?@"YES":@"NO"); NSString *filepath = @"/Users/geek/Desktop/data.plist"; BOOL res = [fileManager fileExistsAtPath:filepath]; LogBool(res)
NSString *filepath1 = @"/Users/geek/Desktop/data.plist"; BOOL isDirectory = NO; BOOL isExist = [fileManager fileExistsAtPath:filepath1 isDirectory:&isDirectory]; if (isExist) { NSLog(@"文件存在"); if (isDirectory) { NSLog(@"文件夾路徑"); }else{ NSLog(@"文件路徑"); } }else{ NSLog(@"給定的路徑不存在"); }
//這是一個系統文件(不可讀) NSString *filePath2 = @"/.DocumentRevisions-V100 "; BOOL isReadable = [fileManager isReadableFileAtPath:filePath2]; if (isReadable) { NSLog(@"文件可讀取"); } else { NSLog(@"文件不可讀取"); }
//系統文件不可寫入 BOOL isWriteAble = [fileManager isWritableFileAtPath:filePath2]; if (isWriteAble) { NSLog(@"文件可寫入"); } else { NSLog(@"文件不可寫入"); }
//系統文件不可刪除 BOOL isDeleteAble = [fileManager isDeletableFileAtPath:filePath2]; if (isDeleteAble) { NSLog(@"文件能夠刪除"); } else { NSLog(@"文件不可刪除"); }
NSError *error = nil; NSDictionary *fileInfo = [fileManager attributesOfItemAtPath:filepath1 error:&error]; // NSLog(@"文件信息:%@,錯誤信息:%@",fileInfo,error); NSLog(@"文件大小:%@",fileInfo[NSFileSize]);
NSString *filePath3 = @"/Users/geek/desktop"; NSArray *subs = [fileManager subpathsAtPath:filePath3]; NSLog(@"Desktop目錄下全部的全部文件和文件夾"); //小竅門:打印數組或者字典,裏面包含中文,直接用%@打印會看不到中文,可用for遍歷訪問 for (NSString *item in subs) { NSLog(@"%@",item); }
NSError *erroe = nil; NSArray *children = [fileManager contentsOfDirectoryAtPath:filePath3 error:&erroe]; NSLog(@"Desktop目錄下的文件和文件夾"); for (NSString *item in children) { NSLog(@"%@",item); }
NSString *filePath1 = @"/Users/geek/Desktop/data.text"; NSData *data = [@"我要學好OC" dataUsingEncoding:NSUTF8StringEncoding]; BOOL createFile = [fileManager createFileAtPath:filePath1 contents:data attributes:nil]; if (createFile) { NSLog(@"文件建立成功"); } else { NSLog(@"文件建立失敗"); }
設置一路建立爲NO,若是文件夾不存在則中止建立文件orm
NSString *filePath2 = @"/Users/geek/Desktop/海賊王"; NSError *error = nil; BOOL createDirectory = [fileManager createDirectoryAtPath:filePath2 withIntermediateDirectories:NO attributes:nil error:&error]; if (createDirectory) { NSLog(@"文件夾建立成功"); } else { NSLog(@"文件夾建立失敗,緣由:%@",error); } //一路建立失敗(文件夾不存在就不建立) NSString *filePath3 = @"/Users/geek/Desktop/海賊王"; BOOL createDirectory1 = [fileManager createDirectoryAtPath:filePath3 withIntermediateDirectories:NO attributes:nil error:&error]; if (createDirectory1) { NSLog(@"文件夾建立成功"); } else { NSLog(@"文件夾建立失敗,緣由:%@",error); }
NSString *filePath4 = @"/Users/geek/Desktop/動漫"; BOOL copyRes = [fileManager copyItemAtPath:filePath3 toPath:filePath4 error:nil]; if (copyRes) { NSLog(@"文件複製成功"); } else { NSLog(@"文件複製失敗"); }
NSString *filePath5 = @"/Users/geek/Downloads/動漫"; BOOL moveRes = [fileManager moveItemAtPath:filePath3 toPath:filePath5 error:nil]; if (moveRes) { NSLog(@"文件移動成功"); } else { NSLog(@"文件移動失敗"); }
//能夠給文件重命名 NSString *filePath6 = @"/Users/geek/Downloads/卡通"; [fileManager moveItemAtPath:filePath5 toPath:filePath6 error:nil];
BOOL deleteRes = [fileManager removeItemAtPath:filePath6 error:nil]; if (deleteRes) { NSLog(@"文件刪除成功"); } else { NSLog(@"文件刪除失敗"); }
//單例方法獲得文件管理者對象 NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *filePath = @"/Users/geek/desktop/delete/"; while (1) { //判斷該文件路徑是否存在 BOOL exist = [fileManager fileExistsAtPath:filePath]; if (exist) { //找出該路徑下的全部文件 NSArray *subs = [fileManager contentsOfDirectoryAtPath:filePath error:nil]; if (subs.count > 0) { for (int i=0; i<subs.count; i++) { NSString *fullFileStr = [NSString stringWithFormat:@"%@%@",filePath,subs[i]]; //判斷文件是否可刪除 BOOL canDelete = [fileManager isDeletableFileAtPath:fullFileStr]; if (canDelete) { [fileManager removeItemAtPath:fullFileStr error:nil]; } } } } //5秒鐘爲週期,開始不斷掃描文件並刪除 [NSThread sleepForTimeInterval:5]; }