Objective-C 目錄與檔案操作
Objective-C 目錄與檔案操作
- NSFileManager 物件執行一些有關目錄與檔案的操作
- 若需具有執行緒安全,需以 alloc 建立且以 NSFileManager 初始,不要用 +defaultManager 方法建立。
- 利用 moveItemAtPath 方法移動檔案,其 toPath 不能為一個已存在的目錄
NSFileManager 用於檔案操作的方法:
| 方法 | 說明 |
| -(BOOL) createFileAtPath: (NSString *) path content: (NSData *) contents attributes: (NSDictionary *) attributes | 建立具有 contents 的內容,以及 attributes 屬性的檔案。若建立成功則回傳 YES,否則回傳 NO |
| -(NSDictionary *) attributesOfItemAtPath: (NSString *) path error: (NSError **) error | 回傳 path 指定的檔案或目錄屬性,若過程有發生任何錯誤,則將錯誤訊息記錄於 error 物件 |
| -(BOOL) removeItemAtPath: (NSString *) path error: (NSError **) error | 若成功刪除 path 指定的檔案、連結、目錄,則回傳 YES,否則將錯誤訊息記錄於 error 物件,並回傳 NO。(此方法將刪除所有子目錄中的內容) |
| -(BOOL) moveItemAtPath: (NSString *) srcPath toPath: (NSString *) dstPath error: (NSError **) error | 將 srcPath 的檔案或目錄移至 dstPath,若移動成功,則回傳 YES,否則回傳 NO。(只有最後一層名稱不同的話,代表更改名稱) |
| -(BOOL) fileExistsAtPath: (NSString *) path | 若 path 指定的檔案存在,則回傳 YES,否則回傳 NO |
NSFileManager 用於目錄操作的方法:
| 方法 | 說明 |
| -(BOOL) createDirectoryAtPath: (NSString *) path attributes: (NSDictionary *) attributes error: (NSError **) error | 建立 path 指定的目錄,且此目錄具有 attributes 的屬性。若成功建立目錄,則回傳 YES,否則回傳 NO |
| -(BOOL) changeCurrentDirectoryPath: (NSString *) path | 改變目前的目錄為 path |
| -(NSString *) currentDirectoryPath | 回傳目前程式所在之路徑 |
| -(NSDirectoryEnumerator *) enumeratorAtPath: (NSString *) path | 回傳 path 指定路徑的內容,內容包含檔案與目錄 |
| -(BOOL)fileExistsAtPath (NSString *) path | 若 path 指定的檔案存在,則回傳 YES,否則回傳 NO |
NSString 路徑操作方法:
| 函式 | 說明 |
| NSString * NSFullUserName (void) | 回傳目前使用者的完整名稱 |
| NSString * NSHomeDirectory (void) | 回傳目前使用者家目錄的路徑 |
| NSString * NSHomeDirectoryForUser (NSString* userName) | 回傳使用者 userName 家目錄的路徑 |
| NSString * NSTemporaryDirectory (void) | 回傳目前使用者暫存目錄的路徑 |
| 方法 | 說明 |
| +(NSString *) pathWithComponents: (NSArray *) components | 以 components 陣列為初始數值,初始為檔案路徑格式 |
| -(NSArray *) pathComponents | 回傳依字串路徑切分的字串陣列 |
| -(NSString *) lastPathComponent | 取得字串路徑最後一個檔案或目錄名稱 |
| -(NSString *) pathExtension | 取得字串路徑的延伸檔名 |
| -(NSString *) stringByAppendingPathComponent: (NSString *) aString | 在字串尾端附加檔案或目錄名稱 |
| -(NSString *) stringByAppendingPathExtension: (NSString *) ext | 在字串尾端附加檔案的延伸檔名 |
| -(NSString *) stringByDeletingLastPathComponent | 刪除字串路徑最後一個檔案或目錄的名稱 |
| -(NSString *) stringByDeletingPathExtension | 刪除字串路徑的延伸檔名 |
| -(NSString *) stringByExpandingTildeInPath | 將字串路徑中的 ~ (目前使用者家目錄) 與 ~user (指定使用者家目錄) 展開為完整路徑 |
| -(NSString *) stringByStandardizingPath | 將字串路徑標準化,展開 ~ 、 ~user,去除 //、 ./ 、 / 、.. |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
| //////////////////////////////////////////////////////////////////// 名稱 : NSFileManager//// 用途 : NSFileManager 使用練習//// 作者 : 飛翔丸子//// 時間 : 2011/10/6//////////////////////////////////////////////////////////////////#import <Foundation/Foundation.h>int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // 建立 NSFileManager 物件 NSFileManager* fmanager = [[NSFileManager alloc] init]; // 取得目前系統的暫存目錄路徑 NSString* tempDirPath = NSTemporaryDirectory(); NSLog(@"目前系統的暫存目錄路徑為 : %@", tempDirPath); // 在暫存目錄路徑後面附加將要新建的檔案名稱 NSString* newFile = [tempDirPath stringByAppendingPathComponent: @"newFile"]; NSLog(@"新建檔案的路徑與檔名 : %@", newFile); // 依照盪案路徑與名稱實際建立檔案 if ([fmanager fileExistsAtPath:newFile] == YES) { NSLog(@"檔案已存在"); }else { [fmanager createFileAtPath: newFile contents:nil attributes: nil]; } // 取得新建檔案的屬性 NSDictionary* attr = [fmanager attributesOfItemAtPath:newFile error:NULL]; NSLog(@"新建檔案的屬性 :"); for(NSString* tempAttr in attr){ NSLog(@"%@ - %@",tempAttr,[attr objectForKey: tempAttr]); } // 移除檔案 NSLog(@"移除新建檔案"); [fmanager removeItemAtPath:newFile error:NULL]; // 建立指定目錄 NSString* dirPath = [fmanager currentDirectoryPath]; NSString* createPath = [dirPath stringByAppendingPathComponent:@"newDirectory"]; NSString* movePath = [dirPath stringByAppendingPathComponent:@"moveDirectory"]; NSString* path; BOOL isDir; [fmanager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:NULL]; [fmanager moveItemAtPath:createPath toPath:movePath error:NULL]; NSLog(@"目前 fmanager 路徑:%@",dirPath); // 取得路徑中所有目錄與檔案 NSLog(@"印出路徑中所有內容:"); NSDirectoryEnumerator* dirEnum = [fmanager enumeratorAtPath: dirPath]; while ((path = [dirEnum nextObject]) != nil) { [fmanager fileExistsAtPath:path isDirectory: &isDir]; NSLog(@"%@名稱為%@",(isDir == YES)? @"目錄": @"檔案",path); } [fmanager removeItemAtPath:movePath error:NULL]; [pool drain]; return 0;} |
留言
張貼留言