ios NSFileManager 用法詳解

SheCaudle 8年前發布 | 16K 次閱讀 Objective-C開發

來自: http://my.oschina.net/daniels/blog/616414


iPhone文件系統NSFileManager講解是本文要介紹的內容,主要是通過iphone文件系統來學習NSFileManager的使用方法,具體內容來看本文詳解。

iPhone文件系統:創建、重命名以及刪除文件,NSFileManager中包含了用來查詢單詞庫目錄、創建、重命名、刪除目錄以及獲取/設置文件屬性的方法(可讀性,可編寫性等等)。

每個程序都會有它自己的沙盒,通過它你可以閱讀/編寫文件。寫入沙盒的文件在程序的進程中將會保持穩定,即便實在程序更新的情況下。

如下所示,你可以在沙盒中定位文件目錄:

  1. //對于錯誤信息  

  2. NSError *error;  

  3. // 創建文件管理器  

  4. NSFileManager *fileMgr = [NSFileManagerdefaultManager];  

  5. //指向文件目錄  

  6. NSString *documentsDirectory= [NSHomeDirectory()   

  7. stringByAppendingPathComponent:@"Documents"];  

  8.  

  9. //創建一個目錄  

  10. [[NSFileManager defaultManager]   createDirectoryAtPath: [NSString stringWithFormat:@"%@/myFolder", NSHomeDirectory()] attributes:nil]; 

創建一個文件

現在我們已經有了文件目錄,我們就能使用這個路徑在沙盒中創建一個新文件并編寫一段代碼:

// File we want to create in the documents directory我們想要創建的文件將會出現在文件目錄中  
// Result is: /Documents/file1.txt結果為:/Documents/file1.txt  
NSString *filePath= [documentsDirectory  
stringByAppendingPathComponent:@"file1.txt"];  
//需要寫入的字符串  
NSString *str= @"iPhoneDeveloper Tips\nhttp://iPhoneDevelopTips,com";  
//寫入文件  
[str writeToFile:filePath atomically:YES   
encoding:NSUTF8StringEncoding error:&error];  
//顯示文件目錄的內容  
NSLog(@"Documentsdirectory: %@",  
[fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);

我們為想要創建的文件構建一條路徑(file1.txt),初始化一個字符串來寫入文件,并列出目錄。最后一行顯示了在我們創建文件之后出現在文件目錄下的一個目錄列表:

對一個文件重命名

想要重命名一個文件,我們需要把文件移到一個新的路徑下。下面的代碼創建了我們所期望的目標文件的路徑,然后請求移動文件以及在移動之后顯示文件目錄。

//通過移動該文件對文件重命名  
NSString *filePath2= [documentsDirectory  
stringByAppendingPathComponent:@"file2.txt"];  
//判斷是否移動  
if ([fileMgr moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)  
NSLog(@"Unable to move file: %@", [error localizedDescription]);  
//顯示文件目錄的內容  
NSLog(@"Documentsdirectory: %@",   
[fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);

在移動了文件之后,輸出結果應該如下圖所示:

刪除一個文件

為了使這個技巧完整,讓我們再一起看下如何刪除一個文件:

//在filePath2中判斷是否刪除這個文件  
if ([fileMgr removeItemAtPath:filePath2 error:&error] != YES)  
NSLog(@"Unable to delete file: %@", [error localizedDescription]);  
//顯示文件目錄的內容  
NSLog(@"Documentsdirectory: %@",  
[fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);

一旦文件被刪除了,正如你所預料的那樣,文件目錄就會被自動清空:

這些示例能教你的,僅僅只是文件處理上的一些皮毛。想要獲得更全面、詳細的講解,你就需要掌握NSFileManager文件的知識。

在開發iPhone程序時,有時候要對文件進行一些操作。而獲取某一個目錄中的所有文件列表,是基本操作之一。通過下面這段代碼,就可以獲取一個目錄內的文件及文件夾列表。

NSFileManager *fileManager = [NSFileManager defaultManager];  
//在這里獲取應用程序Documents文件夾里的文件及文件夾列表  
        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
        NSString *documentDir = [documentPaths objectAtIndex:0];  
        NSError *error = nil;  
        NSArray *fileList = [[NSArray alloc] init];  
//fileList便是包含有該文件夾下所有文件的文件名及文件夾名的數組  
        fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error];

以下這段代碼則可以列出給定一個文件夾里的所有子文件夾名

NSMutableArray *dirArray = [[NSMutableArray alloc] init];  
        BOOL isDir = NO;  
//在上面那段程序中獲得的fileList中列出文件夾名  
        for (NSString *file in fileList) {  
                NSString *path = [documentDir stringByAppendingPathComponent:file];  
                [fileManager fileExistsAtPath:path isDirectory:(&isDir)];  
                if (isDir) {  
                        [dirArray addObject:file];  
                }  
                isDir = NO;  
        }  
        NSLog(@"Every Thing in the dir:%@",fileList);  
        NSLog(@"All folders:%@",dirArray);

 

 本文由用戶 SheCaudle 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!