目錄操作可參考 http://kusodir.blogspot.tw/2011/10/objective-c.html
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,去除 //、 ./ 、 / 、.. |
基本使用例可參考
http://www.techotopia.com/index.php/Working_with_Files_on_iOS_4_%28Xcode_4%29
basic example
//取得可讀寫的路徑NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *path = [pathList objectAtIndex:0];//加上檔名path = [path stringByAppendingPathComponent:filename];NSLog([@"存取檔案的路徑:" stringByAppendingString: path]);//寫入檔案NSString *data = @"file data";[data writeToFile:path atomically:NO encoding:NSUTF8StringEncoding error:nil]; // 取得新建檔案的屬性 NSDictionary* attr = [fmanager attributesOfItemAtPath:path error:NULL]; NSLog(@"新建檔案的屬性 :"); for(NSString* tempAttr in attr){ NSLog(@"%@ - %@",tempAttr,[attr objectForKey: tempAttr]); }//讀取檔案NSString *rdata = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];NSLog([@"檔案內容:" stringByAppendingString: rdata]);//判斷檔案是否存在if([[NSFileManager defaultManager] fileExistsAtPath:path]) { NSLog([@"檔案存在,開始刪除:" stringByAppendingString: path]); //刪除檔案 [[NSFileManager defaultManager] removeItemAtPath:path error:nil];}對於檔案的屬性,操作上需要瞭解及注意
官方文件參考如下
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsfilemanager_Class/Reference/Reference.html#//apple_ref/doc/constant_group/File_Attribute_Keys
使用範例
// Get the size of newfile2
if ((attr = [fm fileAttributesAtPath: @ ” newfile2 ” traverseLink: NO]) == nil) {NSLog (@ ” Couldn ’ t get file attributes! ” );
return 5;
}
NSLog (@ ” File size is %i bytes ” , [[attr objectForKey: NSFileSize] intValue]);
屬性變數 (objectForKey: xxx)
NSFileAppendOnly- NSNumber... boolean
NSFileCreationDate- NSDate when the file was created (if supported)
NSFileDeviceIdentifier- NSNumber (identifies the device on which the file is stored)
NSFileExtensionHidden- NSNumber... boolean
-
NSFileGroupOwnerAccountName - NSString name of the file group
-
NSFileGroupOwnerAccountID - NSNumber ID of the file group
NSFileHFSCreatorCode- NSNumber not used
NSFileHFSTypeCode- NSNumber not used
NSFileImmutable- NSNumber... boolean
NSFileModificationDate- NSDate when the file was last modified
NSFileOwnerAccountName- NSString name of the file owner
NSFileOwnerAccountID- NSNumber ID of the file owner
NSFilePosixPermissions- NSNumber posix access permissions mask
NSFileReferenceCount- NSNumber number of links to this file
NSFileSize- NSNumber size of the file in bytes
NSFileSystemFileNumber- NSNumber the identifier for the file on the filesystem
NSFileSystemNumber- NSNumber the filesystem on which the file is stored
NSFileType- NSString the type of file
沒有留言:
張貼留言