自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

關于iPhone文件系統(tǒng)NSFileManager講解

移動開發(fā) iOS
iPhone文件系統(tǒng)NSFileManager講解是本文要介紹的內容,主要是通過iphone文件系統(tǒng)來學習NSFileManager的使用方法,具體內容來看本文詳解。

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

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

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

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

  1. //對于錯誤信息  
  2. NSError *error;  
  3. // 創(chuàng)建文件管理器  
  4. NSFileManager *fileMgr = [NSFileManagerdefaultManager];  
  5. //指向文件目錄  
  6. NSString *documentsDirectory= [NSHomeDirectory()   
  7. stringByAppendingPathComponent:@"Documents"];  
  8.  
  9. //創(chuàng)建一個目錄  
  10. [[NSFileManager defaultManager]   createDirectoryAtPath: [NSString stringWithFormat:@"%@/myFolder", NSHomeDirectory()] attributes:nil]; 

創(chuàng)建一個文件

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

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

我們?yōu)橄胍獎?chuàng)建的文件構建一條路徑(file1.txt),初始化一個字符串來寫入文件,并列出目錄。最后一行顯示了在我們創(chuàng)建文件之后出現(xiàn)在文件目錄下的一個目錄列表:

對一個文件重命名

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

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

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

刪除一個文件

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

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

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

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

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

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

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

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

小結:關于iPhone文件系統(tǒng)NSFileManager講解的內容介紹完了,希望通過本文的學習能對你有所幫助!

責任編輯:zhaolei 來源: 網(wǎng)易博客
相關推薦

2010-04-23 17:09:25

Aix文件系統(tǒng)

2011-07-29 13:27:48

iPhone 開發(fā) Nib

2010-05-04 14:36:40

Unix文件系統(tǒng)

2010-05-05 17:46:32

Unix文件系統(tǒng)

2010-04-08 15:58:24

Unix操作系統(tǒng)

2009-12-18 16:18:15

Fedora proc

2010-04-22 14:45:31

Aix操作系統(tǒng)

2009-12-22 11:30:38

Linux操作系統(tǒng)

2009-10-22 12:09:57

linux文件系統(tǒng)

2009-10-28 13:29:14

Linux文件系統(tǒng)安裝

2010-06-22 16:18:54

2014-06-24 15:24:52

Moosefs分布式集群

2009-12-10 13:35:25

Linux操作系統(tǒng)

2009-10-28 14:29:40

linux文件系統(tǒng)

2020-07-22 14:53:06

Linux系統(tǒng)虛擬文件

2010-06-22 17:05:04

Autoconf教程

2018-08-14 10:44:58

HadoopHDFS命令

2011-01-13 14:10:30

Linux文件系統(tǒng)

2018-08-24 10:10:25

Linux文件系統(tǒng)技術

2019-09-20 10:04:45

Linux系統(tǒng)虛擬文件
點贊
收藏

51CTO技術棧公眾號