目錄管理(Create/Copy/Move/Remove/Attribute)
NSFileManager 照名稱當然也是提供最基本目錄管理:拷貝、移動、刪除功能,以上這些與檔案管理其實是類似的,目錄管理增加了一項建立目錄的功能,當然也會依照路徑表示方式,使用2種方式介紹。
傳統路徑表示
傳統路徑所代表的就是一般路徑表示,Mac是使用Unix的路徑表示方式。
建立目錄 Create
建立目錄時,必需要注意withIntermediateDirectories
這個傳入的值,當YES
,不管您指定的目錄名稱中間的目錄是否存在,它都會自動不存在的目錄並將所有目錄建立完成,例如,當我要建立目錄名稱為dir1
,並且指定的路徑是/tmp/dira/dir1
,中間的dira
目錄並不存在,但指定withIntermediateDirectories: YES
就會先建立dira
再建立dir1
,反之只要建立目錄過程碰到 路徑本身不存在或您指定的目錄已經存在 時就會停止建立並返回建立失敗值為NO
。
//-----------start----------- NSFileManager *filemanager; filemanager = [NSFileManager defaultManager]; if ([filemanager createDirectoryAtPath: @"/tmp/dir1" withIntermediateDirectories: YES attributes: nil error: NULL] == YES) NSLog (@"建立成功!"); else NSLog (@"建立失敗!"); //------------end------------
拷貝目錄 Copy
//-----------start----------- NSFileManager *filemanager; filemanager = [NSFileManager defaultManager]; if ([filemanager copyItemAtPath: @"/tmp/dir1" toPath: @"/tmp/dir2" error: NULL] == YES) NSLog (@"拷貝成功!"); else NSLog (@"拷貝失敗!"); //------------end------------
刪除目錄 Remove
//-----------start----------- NSFileManager *filemanager; filemanager = [NSFileManager defaultManager]; if ([filemanager removeItemAtPath:@"/tmp/dir2" error: NULL] == YES) NSLog (@"刪除成功!"); else NSLog (@"刪除失敗"); //------------end------------
移動檔案 Move
//-----------start----------- NSFileManager *filemanager; filemanager = [NSFileManager defaultManager]; if ([filemanager moveItemAtPath:@"/tmp/dir1" toPath: @"/tmp/dir2" error: NULL] == YES) NSLog (@"移動成功"); else NSLog (@"移動失敗"); //------------end------------
網路路徑表示
網路路徑表示是將整個系統路徑用網址的方式表示出來,例如:
/tmp/dir1
經過網路路徑表示方式會變成:
file:///tmp/dir1
當然這是可以經過NSURL
將傳統路徑轉換成網路路徑:
//-----------start----------- [NSURL fileURLWithPath:@"/tmp/dir1"]; //------------end------------
建立目錄 Create
建立目錄時規則及參數用法先前已經有介紹,這裡不再描述。
//-----------start----------- NSFileManager *filemanager; NSURL *oldDir = [NSURL fileURLWithPath:@"/tmp/dir1"]; NSURL *newDir= [NSURL fileURLWithPath:@"/tmp/dir2"]; filemanager = [NSFileManager defaultManager]; if ([filemanager createDirectoryAtURL: oldDir withIntermediateDirectories: YES attributes: nil error: NULL] == YES) NSLog (@"建立成功!"); else NSLog (@"建立失敗!"); //------------end------------
拷貝目錄 Copy
//-----------start----------- NSFileManager *filemanager; NSURL *oldDir = [NSURL fileURLWithPath:@"/tmp/dir1"]; NSURL *newDir= [NSURL fileURLWithPath:@"/tmp/dir2"]; filemanager = [NSFileManager defaultManager]; if ([filemanager copyItemAtURL: oldDir toURL: newDir error: NULL] == YES) NSLog (@"拷貝成功"); else NSLog (@"拷貝失敗"); //------------end------------
刪除目錄 Remove
//-----------start----------- NSFileManager *filemanager; NSURL *oldDir = [NSURL fileURLWithPath:@"/tmp/dir1"]; NSURL *newDir= [NSURL fileURLWithPath:@"/tmp/dir2"]; filemanager = [NSFileManager defaultManager]; if ([filemanager removeItemAtURL:newDir error: NULL] == YES) NSLog (@"刪除成功"); else NSLog (@"刪除失敗"); //------------end------------
移動目錄 Move
//-----------start----------- NSFileManager *filemanager; NSURL *oldDir = [NSURL fileURLWithPath:@"/tmp/dir1"]; NSURL *newDir= [NSURL fileURLWithPath:@"/tmp/dir2"]; filemanager = [NSFileManager defaultManager]; if ([filemanager moveItemAtURL: oldDir toURL: newDir error: NULL] == YES) NSLog (@"移動成功"); else NSLog (@"移動失敗"); //------------end------------
顯示錯誤原因
不管做什麼樣的目錄管理,如果返回值為NO
可能無法判斷問題的原因,上面所提供的都未處理error
,下面例子會舉一個方式將錯誤顯示:
//-----------start----------- NSFileManager *filemanager; NSError *error; filemanager = [NSFileManager defaultManager]; if ([filemanager moveItemAtPath:@"/tmp/dir1" toPath: @"/tmp/dir2" error: &error] == YES) NSLog (@"移動成功"); else { NSLog (@"移動失敗"); NSLog(@"%@",[error userInfo]); } //------------end------------
輸出結果:
2014-02-22 08:28:03.052 test1[7372:303] 移動失敗 2014-02-22 08:28:03.074 test1[7372:303] { NSDestinationFilePath = "/tmp/dir2"; NSFilePath = "/tmp/dir1"; NSSourceFilePathErrorKey = "/tmp/dir1"; NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=17 \"The operation couldn\U2019t be completed. File exists\""; NSUserStringVariant = ( Move ); }
從輸出結果的NSUnderlyingError
看到原因是File exists
,也就是目錄已經存在,無法完成,我們可以將程式改成:
//-----------start----------- NSFileManager *filemanager; NSError *error; filemanager = [NSFileManager defaultManager]; if ([filemanager moveItemAtPath:@"/tmp/dir1" toPath: @"/tmp/dir2" error: &error] == YES) NSLog (@"移動成功"); else { NSLog (@"移動失敗"); NSLog(@"%@",[error localizedDescription] ); NSLog(@"%@",[error localizedFailureReason] ); NSLog(@"%@",[[[error userInfo] valueForKey:NSUnderlyingErrorKey] localizedDescription]); NSLog(@"%@",[[[error userInfo] valueForKey:NSUnderlyingErrorKey] localizedFailureReason]); } //------------end------------
錯誤的原因內容顯示在程式列出四種方法,選擇其中適合自已程式使用的方式。
NSLog(@"%@",[error localizedDescription] ); NSLog(@"%@",[error localizedFailureReason] ); NSLog(@"%@",[[[error userInfo] valueForKey:NSUnderlyingErrorKey] localizedDescription]); NSLog(@"%@",[[[error userInfo] valueForKey:NSUnderlyingErrorKey] localizedFailureReason]);
輸出結果:
22014-02-22 08:46:42.557 test1[8025:303] 移動失敗 2014-02-22 08:46:42.574 test1[8025:303] “dir1” couldn’t be moved to “tmp” because an item with the same name already exists. 2014-02-22 08:46:42.578 test1[8025:303] A file with the name “dir1” already exists. 2014-02-22 08:46:42.592 test1[8025:303] The operation couldn’t be completed. File exists 2014-02-22 08:46:42.604 test1[8025:303] File exists