偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

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

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

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

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

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

創(chuàng)建一個(gè)文件

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

  1. // File we want to create in the documents directory我們想要?jiǎng)?chuàng)建的文件將會出現(xiàn)在文件目錄中  
  2. // Result is: /Documents/file1.txt結(jié)果為:/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. //顯示文件目錄的內(nèi)容  
  11. NSLog(@"Documentsdirectory: %@",  
  12. [fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]); 

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

對一個(gè)文件重命名

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

  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. //顯示文件目錄的內(nèi)容  
  8. NSLog(@"Documentsdirectory: %@",   
  9. [fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]); 

在移動了文件之后,輸出結(jié)果應(yīng)該如下圖所示:

刪除一個(gè)文件

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

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

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

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

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

  1. NSFileManager *fileManager = [NSFileManager defaultManager];  
  2. //在這里獲取應(yīng)用程序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]; 

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

  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); 

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

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

2010-04-23 17:09:25

Aix文件系統(tǒng)

2011-07-29 13:27:48

iPhone 開發(fā) Nib

2010-05-05 17:46:32

Unix文件系統(tǒng)

2010-05-04 14:36:40

Unix文件系統(tǒng)

2010-04-08 15:58:24

Unix操作系統(tǒng)

2009-12-18 16:18:15

Fedora proc

2009-12-22 11:30:38

Linux操作系統(tǒng)

2010-04-22 14:45:31

Aix操作系統(tǒng)

2009-10-28 13:29:14

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

2009-10-22 12:09:57

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)

2019-09-20 10:04:45

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

2018-08-24 10:10:25

Linux文件系統(tǒng)技術(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號