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

詳解iPhone文件讀寫(xiě)系統(tǒng)操作教程

移動(dòng)開(kāi)發(fā) iOS
本文介紹的是iPhone文件系統(tǒng)操作的創(chuàng)建、重命名以及刪除文件,主要介紹的是文件的讀寫(xiě),詳細(xì)的講述了每一個(gè)從操作,來(lái)看詳細(xì)內(nèi)容講解。

iPhone文件讀寫(xiě)系統(tǒng)操作教程是本文要介紹的內(nèi)容,對(duì)于一個(gè)運(yùn)行在iPhone得app,它只能訪問(wèn)自己根目錄下得一些文件(所謂sandbox).一個(gè)app發(fā)布到iPhone上后,它得目錄結(jié)構(gòu)如下:
 
1、其中得 app root 可以用 NSHomeDirectory() 訪問(wèn)到;

2、Documents 目錄就是我們可以用來(lái)寫(xiě)入并保存文件得地方,一般可通過(guò):

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
  2. NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0]; 

得到。

3、tmp 目錄我們可以在里面寫(xiě)入一些程序運(yùn)行時(shí)需要用得數(shù)據(jù),里面寫(xiě)入得數(shù)據(jù)在程序退出后會(huì)沒(méi)有??梢酝ㄟ^(guò)NSString *NSTemporaryDirectory(void); 方法得到;

4、文件一些主要操作可以通過(guò)NSFileManage 來(lái)操作,可以通過(guò) [NSFileManger defaultManger] 得到它得實(shí)例。

相關(guān)得一些操作:

創(chuàng)建一個(gè)目錄:比如要在Documents下面創(chuàng)建一個(gè)test目錄,

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  2. NSString *documentsDirectory = [paths objectAtIndex:0];  
  3. NSLog(@”%@”,documentsDirectory);  
  4. NSFileManager *fileManage = [NSFileManager defaultManager];  
  5. NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@“test”];  
  6. BOOL ok = [fileManage createDirectoryAtPath:myDirectory attributes:nil]; 

取得一個(gè)目錄下得所有文件名:(如上面的myDirectory)可用

  1. NSArray *file = [fileManager subpathsOfDirectoryAtPath: myDirectory error:nil];  
  2. 或  
  3. NSArray *files = [fileManager subpathsAtPath: myDirectory ]; 

讀取某個(gè)文件:

  1. NSData *data = [fileManger contentsAtPath:myFilePath];//myFilePath是包含完整路徑的文件名或直接用NSData 的類方法:  
  2. NSData *data = [NSData dataWithContentOfPath:myFilePath]; 

保存某個(gè)文件:

可以用 NSFileManager的

  1. - (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;  
  2.  
  3. 或 NSData 的  
  4.  
  5. - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;  
  6. - (BOOL)writeToFile:(NSString *)path options:(NSUInteger)writeOptionsMask error:(NSError **)errorPtr; 

NSFileManager中包含了用來(lái)查詢單詞庫(kù)目錄、創(chuàng)建、重命名、刪除目錄以及獲取/設(shè)置文件屬性的方法(可讀性,可編寫(xiě)性等等)。

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

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

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

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

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

  1. // File we want to create in the documents directory我們想要?jiǎng)?chuàng)建的文件將會(huì)出現(xiàn)在文件目錄中  
  2. // Result is: /Documents/file1.txt結(jié)果為:/Documents/file1.txt  
  3. NSString *filePath= [documentsDirectory  
  4. stringByAppendingPathComponent:@"file1.txt"];  
  5. //需要寫(xiě)入的字符串  
  6. NSString *str= @"iPhoneDeveloper Tips\nhttp://iPhoneDevelopTips,com";  
  7. //寫(xiě)入文件  
  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è)字符串來(lái)寫(xiě)入文件,并列出目錄。***一行顯示了在我們創(chuàng)建文件之后出現(xiàn)在文件目錄下的一個(gè)目錄列表:

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

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

  1. //通過(guò)移動(dòng)該文件對(duì)文件重命名  
  2. NSString *filePath2= [documentsDirectory  
  3. stringByAppendingPathComponent:@"file2.txt"];  
  4. //判斷是否移動(dòng)  
  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]);  
  10. 在移動(dòng)了文件之后,輸出結(jié)果應(yīng)該如下圖所示:  
  11. 刪除一個(gè)文件  
  12. 為了使這個(gè)技巧完整,讓我們?cè)僖黄鹂聪氯绾蝿h除一個(gè)文件:  
  13. //在filePath2中判斷是否刪除這個(gè)文件  
  14. if ([fileMgr removeItemAtPath:filePath2 error:&error] != YES)  
  15. NSLog(@"Unable to delete file: %@", [error localizedDescription]);  
  16. //顯示文件目錄的內(nèi)容  
  17. NSLog(@"Documentsdirectory: %@",  
  18. [fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]); 

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

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

在開(kāi)發(fā)iPhone程序時(shí),有時(shí)候要對(duì)文件進(jìn)行一些操作。而獲取某一個(gè)目錄中的所有文件列表,是基本操作之一。通過(guò)下面這段代碼,就可以獲取一個(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];  
  9.  
  10. 以下這段代碼則可以列出給定一個(gè)文件夾里的所有子文件夾名  
  11.  
  12. NSMutableArray *dirArray = [[NSMutableArray alloc] init];  
  13.         BOOL isDir = NO;  
  14. //在上面那段程序中獲得的fileList中列出文件夾名  
  15.         for (NSString *file in fileList) {  
  16.                 NSString *path = [documentDir stringByAppendingPathComponent:file];  
  17.                 [fileManager fileExistsAtPath:path isDirectory:(&isDir)];  
  18.                 if (isDir) {  
  19.                         [dirArray addObject:file];  
  20.                 }  
  21.                 isDir = NO;  
  22.         }  
  23.         NSLog(@"Every Thing in the dir:%@",fileList);  
  24.         NSLog(@"All folders:%@",dirArray);  

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

責(zé)任編輯:zhaolei 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-08-22 12:01:38

iPhone開(kāi)發(fā)文件

2011-07-27 15:47:09

iPhone Simulator 文件

2011-08-03 17:44:57

iPhone App 文件

2010-07-16 09:06:57

Perl文件

2011-08-01 09:50:46

iPhone 獲取對(duì)象 UIView

2011-08-23 17:06:03

2009-12-10 14:27:07

Linux操作系統(tǒng)

2009-08-13 09:34:55

C#讀寫(xiě)ini文件

2011-07-29 11:28:58

iPhone開(kāi)發(fā)

2011-07-18 13:37:53

2011-07-06 16:25:10

iPhone 程序 調(diào)用

2011-08-12 14:33:06

iPhone緩存文件

2011-07-22 15:59:15

iPhone 聲音 文件

2011-07-27 17:24:31

iPhone NSXMLParse XML

2011-05-12 08:49:58

iPhone SDKXcode

2011-07-26 18:11:56

iPhone Sqlite 數(shù)據(jù)庫(kù)

2019-11-19 11:20:25

Python數(shù)據(jù)結(jié)構(gòu)Windows

2024-04-25 12:35:14

JSONC#開(kāi)發(fā)

2011-07-18 13:11:53

2013-03-20 10:35:11

文件系統(tǒng)
點(diǎn)贊
收藏

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