關(guān)于iPhone文件系統(tǒng)NSFileManager講解
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í)在程序更新的情況下。
如下所示,你可以在沙盒中定位文件目錄:
- //對于錯(cuò)誤信息
- NSError *error;
- // 創(chuàng)建文件管理器
- NSFileManager *fileMgr = [NSFileManagerdefaultManager];
- //指向文件目錄
- NSString *documentsDirectory= [NSHomeDirectory()
- stringByAppendingPathComponent:@"Documents"];
- //創(chuàng)建一個(gè)目錄
- [[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/myFolder", NSHomeDirectory()] attributes:nil];
創(chuàng)建一個(gè)文件
現(xiàn)在我們已經(jīng)有了文件目錄,我們就能使用這個(gè)路徑在沙盒中創(chuàng)建一個(gè)新文件并編寫一段代碼:
- // File we want to create in the documents directory我們想要?jiǎng)?chuàng)建的文件將會出現(xiàn)在文件目錄中
- // Result is: /Documents/file1.txt結(jié)果為:/Documents/file1.txt
- NSString *filePath= [documentsDirectory
- stringByAppendingPathComponent:@"file1.txt"];
- //需要寫入的字符串
- NSString *str= @"iPhoneDeveloper Tips\nhttp://iPhoneDevelopTips,com";
- //寫入文件
- [str writeToFile:filePath atomically:YES
- encoding:NSUTF8StringEncoding error:&error];
- //顯示文件目錄的內(nèi)容
- NSLog(@"Documentsdirectory: %@",
- [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)文件的路徑,然后請求移動文件以及在移動之后顯示文件目錄。
- //通過移動該文件對文件重命名
- NSString *filePath2= [documentsDirectory
- stringByAppendingPathComponent:@"file2.txt"];
- //判斷是否移動
- if ([fileMgr moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)
- NSLog(@"Unable to move file: %@", [error localizedDescription]);
- //顯示文件目錄的內(nèi)容
- NSLog(@"Documentsdirectory: %@",
- [fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);
在移動了文件之后,輸出結(jié)果應(yīng)該如下圖所示:
刪除一個(gè)文件
為了使這個(gè)技巧完整,讓我們再一起看下如何刪除一個(gè)文件:
- //在filePath2中判斷是否刪除這個(gè)文件
- if ([fileMgr removeItemAtPath:filePath2 error:&error] != YES)
- NSLog(@"Unable to delete file: %@", [error localizedDescription]);
- //顯示文件目錄的內(nèi)容
- NSLog(@"Documentsdirectory: %@",
- [fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);
一旦文件被刪除了,正如你所預(yù)料的那樣,文件目錄就會被自動清空:
這些示例能教你的,僅僅只是文件處理上的一些皮毛。想要獲得更全面、詳細(xì)的講解,你就需要掌握NSFileManager文件的知識。
在開發(fā)iPhone程序時(shí),有時(shí)候要對文件進(jìn)行一些操作。而獲取某一個(gè)目錄中的所有文件列表,是基本操作之一。通過下面這段代碼,就可以獲取一個(gè)目錄內(nèi)的文件及文件夾列表。
- NSFileManager *fileManager = [NSFileManager defaultManager];
- //在這里獲取應(yīng)用程序Documents文件夾里的文件及文件夾列表
- NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentDir = [documentPaths objectAtIndex:0];
- NSError *error = nil;
- NSArray *fileList = [[NSArray alloc] init];
- //fileList便是包含有該文件夾下所有文件的文件名及文件夾名的數(shù)組
- fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error];
以下這段代碼則可以列出給定一個(gè)文件夾里的所有子文件夾名
- 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);
小結(jié):關(guān)于iPhone文件系統(tǒng)NSFileManager講解的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!