成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

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

2、Documents 目錄就是我們可以用來寫入并保存文件得地方,一般可通過:

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

得到。

3、tmp 目錄我們可以在里面寫入一些程序運(yùn)行時(shí)需要用得數(shù)據(jù),里面寫入得數(shù)據(jù)在程序退出后會沒有。可以通過NSString *NSTemporaryDirectory(void); 方法得到;

4、文件一些主要操作可以通過NSFileManage 來操作,可以通過 [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中包含了用來查詢單詞庫目錄、創(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. //創(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è)新文件并編寫一段代碼:

  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)文件的路徑,然后請求移動(dòng)文件以及在移動(dòng)之后顯示文件目錄。

  1. //通過移動(dòng)該文件對文件重命名  
  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è)技巧完整,讓我們再一起看下如何刪除一個(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ù)料的那樣,文件目錄就會被自動(dòng)清空:

這些示例能教你的,僅僅只是文件處理上的一些皮毛。想要獲得更全面、詳細(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];  
  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)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!

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

2011-08-22 12:01:38

iPhone開發(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 獲取對象 UIView

2011-08-23 17:06:03

2009-12-10 14:27:07

Linux操作系統(tǒng)

2009-08-13 09:34:55

C#讀寫ini文件

2011-07-29 11:28:58

iPhone開發(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ù)庫

2019-11-19 11:20:25

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

2024-04-25 12:35:14

JSONC#開發(fā)

2011-07-18 13:11:53

2013-03-20 10:35:11

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

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

主站蜘蛛池模板: 久久天堂 | 国产精品国产三级国产aⅴ无密码 | 91视频进入| 亚洲午夜精品 | 国产视频一区在线 | 超碰在线国产 | 日韩福利在线观看 | 毛片一级片 | 久久神马 | 免费一区在线 | 日韩欧美一级精品久久 | 五月天婷婷综合 | 成人免费观看男女羞羞视频 | 99久久精品国产一区二区三区 | 在线观看亚洲专区 | 在线观看www高清视频 | 久久久青草婷婷精品综合日韩 | 欧美11一13sex性hd | av小说在线 | 97免费视频在线观看 | 成人a视频片观看免费 | 真人毛片 | 日韩成人在线观看 | 中文字幕亚洲无线 | 欧美1区 | 久久高清| 国产成人在线视频免费观看 | 欧美性网| 日韩成人中文字幕 | 91久久国产综合久久 | 久草视频观看 | 日韩电影中文字幕 | 99精品欧美一区二区三区 | 午夜激情在线 | 九九视频在线观看视频6 | 成人欧美一区二区三区在线播放 | 国产精品色 | 国产一区二区三区在线 | 亚洲国产精品一区二区三区 | 男人影音 | 久久国产秒 |