【代碼筆記】iOS-NSFileManager,ios-nsfilemanager
一,代碼。
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //擷取應用沙箱根路徑 [self dirHome]; //擷取Documents目錄 [self dirDoc]; //擷取Library目錄 [self dirLib]; //擷取Cache目錄 [self dirCache]; //建立檔案夾 [self createDir]; //建立檔案 [self createFile]; //寫資料到檔案 [self writeFile]; //讀檔案 [self readFile]; //檔案屬性 [self fileAttriutes]; //刪除檔案 [self deleteFile]; }#pragma -mark -funcitons//擷取應用沙箱根路徑-(void)dirHome{ NSString *dirHome=NSHomeDirectory(); NSLog(@"應用沙箱根路徑: %@",dirHome);}//擷取Documents目錄-(NSString *)dirDoc{ //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSLog(@"Documents目錄: %@",documentsDirectory); return documentsDirectory;}//擷取Library目錄-(void)dirLib{ //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryDirectory = [paths objectAtIndex:0]; NSLog(@"Library目錄: %@",libraryDirectory);}//擷取Cache目錄-(void)dirCache{ NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachePath = [cacPath objectAtIndex:0]; NSLog(@"Cache目錄: %@",cachePath);}//擷取Tmp目錄-(void)dirTmp{ //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"]; NSString *tmpDirectory = NSTemporaryDirectory(); NSLog(@"Tmp目錄: %@",tmpDirectory);}//建立檔案夾-(void)createDir{ NSString *documentsPath =[self dirDoc]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; // 建立目錄 BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil]; if (res) { NSLog(@"檔案夾建立成功"); }else{ NSLog(@"檔案夾建立失敗"); }}//建立檔案-(void)createFile{ NSString *documentsPath =[self dirDoc]; NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil]; if (res) { NSLog(@"檔案建立成功: %@" ,testPath); }else NSLog(@"檔案建立失敗");}//寫資料到檔案-(void)writeFile{ NSString *documentsPath =[self dirDoc]; NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; NSString *content=@"測試寫入內容!"; BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; if (res) { NSLog(@"檔案寫入成功"); }else NSLog(@"檔案寫入失敗");}//讀檔案-(void)readFile{ NSString *documentsPath =[self dirDoc]; NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; // NSData *data = [NSData dataWithContentsOfFile:testPath]; // NSLog(@"檔案讀取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil]; NSLog(@"檔案讀取成功: %@",content);}//檔案屬性-(void)fileAttriutes{ NSString *documentsPath =[self dirDoc]; NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil]; NSArray *keys; id key, value; keys = [fileAttributes allKeys]; int count = [keys count]; for (int i = 0; i < count; i++) { key = [keys objectAtIndex: i]; value = [fileAttributes objectForKey: key]; NSLog (@"Key: %@ for value: %@", key, value); }}//刪除檔案-(void)deleteFile{ NSString *documentsPath =[self dirDoc]; NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; BOOL res=[fileManager removeItemAtPath:testPath error:nil]; if (res) { NSLog(@"檔案刪除成功"); }else NSLog(@"檔案刪除失敗"); NSLog(@"檔案是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");}@end
二,輸出。
2015-10-23 11:17:54.335 NSFileManager[5578:133206] 應用沙箱根路徑: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F39802015-10-23 11:17:54.336 NSFileManager[5578:133206] Documents目錄: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents2015-10-23 11:17:54.336 NSFileManager[5578:133206] Library目錄: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Library2015-10-23 11:17:54.337 NSFileManager[5578:133206] Cache目錄: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Library/Caches2015-10-23 11:17:54.337 NSFileManager[5578:133206] Documents目錄: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents2015-10-23 11:17:54.337 NSFileManager[5578:133206] 檔案夾建立成功2015-10-23 11:17:54.337 NSFileManager[5578:133206] Documents目錄: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents2015-10-23 11:17:54.349 NSFileManager[5578:133206] 檔案建立成功: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents/test/test.txt2015-10-23 11:17:54.349 NSFileManager[5578:133206] Documents目錄: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents2015-10-23 11:17:54.350 NSFileManager[5578:133206] 檔案寫入成功2015-10-23 11:17:54.350 NSFileManager[5578:133206] Documents目錄: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents2015-10-23 11:17:54.351 NSFileManager[5578:133206] 檔案讀取成功: 測試寫入內容!2015-10-23 11:17:54.351 NSFileManager[5578:133206] Documents目錄: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents2015-10-23 11:17:54.352 NSFileManager[5578:133206] Key: NSFileOwnerAccountID for value: 5012015-10-23 11:17:54.352 NSFileManager[5578:133206] Key: NSFileSystemFileNumber for value: 132768632015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFileExtensionHidden for value: 02015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFileSystemNumber for value: 167772202015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFileSize for value: 212015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFileGroupOwnerAccountID for value: 802015-10-23 11:17:54.353 NSFileManager[5578:133206] Key: NSFilePosixPermissions for value: 4202015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileCreationDate for value: 2015-10-23 03:17:54 +00002015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileExtendedAttributes for value: { "com.apple.TextEncoding" = <7574662d 383b3133 34323137 393834>;}2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileType for value: NSFileTypeRegular2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileGroupOwnerAccountName for value: admin2015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileReferenceCount for value: 12015-10-23 11:17:54.355 NSFileManager[5578:133206] Key: NSFileModificationDate for value: 2015-10-23 03:17:54 +00002015-10-23 11:17:54.356 NSFileManager[5578:133206] Documents目錄: /Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/19A5C4E6-8CDD-428A-B08C-FC68363F3980/Documents2015-10-23 11:17:54.356 NSFileManager[5578:133206] 檔案刪除成功2015-10-23 11:17:54.356 NSFileManager[5578:133206] 檔案是否存在: NO