閱讀260 返回首頁    go 阿裏雲 go 技術社區[雲棲]


iPhone文件係統NSFileManager講解

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

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

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

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

 

  • //對於錯誤信息
  • NSError *error;
  • // 創建文件管理器
  • NSFileManager *fileMgr = [NSFileManagerdefaultManager];
  • //指向文件目錄
  • NSString *documentsDirectory= [NSHomeDirectory()
  • stringByAppendingPathComponent:@"Documents"];

  • //創建一個目錄
  • [[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\nhttps://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);

    轉自iOS分享網https://iosshare.cn

最後更新:2017-04-02 15:14:44

  上一篇:go 程序員之路——一個老程序員對剛上大學的學弟學妹的忠告
  下一篇:go spring3mvc框架開發中resin和tomcat默認servelt配置處理靜態資源