[IOS開發教程] NSfileManager的使用方法
總結了一下NSFileManager的一些常用方法,
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask,YES);
NSString *documentDirectory = [paths objectAtIndex:];http://www.kmnk03.com/hxpfk/qcd/392.html NSLog(@"%@", documentDirectory);//路徑檢視
//1.建立資料夾 NSFileManager *fileManager = [NSFileManagerdefaultManager]; NSString *testDirectory = [documentDirectoryhttp://www.kmnk03.com/hxpfk/qcd/393.html stringByAppendingPathComponent:@"testbug1"];//建立資料夾 //建立目錄 [fileManager http://www.kmnk03.com/hxpfk/qcd/394.htmlcreateDirectoryAtPath:testDirectorywithIntermediateDirectories:YESattributes:nilerror:nil];
//2.建立檔案 NSString *test11 = [http://www.kmnk03.com/hxpfk/qcd/395.htmltestDirectory stringByAppendingPathComponent:@"test111.txt"]; NSString *test22 = [testDirectory http://www.kmnk03.com/hxpfk/qcd/396.htmlstringByAppendingPathComponent:@"test222.txt"]; NSString *string = @"寫入內容1"; [fileManager createFileAtPath:test11contents:[string dataUsingEncoding:NSUTF8StringEncoding]attributes:nil]; [fileManager createFileAtPath:test22contents:[string dataUsingEncoding:NSUTF8StringEncoding]attributes:nil];
//3.檢視Document裡所有檔案 NSArray *files = [fileManagerhttp://www.kmnk03.com/hxpfk/qcd/397.html subpathsAtPath:documentDirectory]; //4.檢視目錄裡所有子檔案 NSArray *subfiles = [http://www.kmnk03.com/hxpfk/qcd/398.htmlfileManager subpathsAtPath:testDirectory]; NSLog(@"%@", subfiles); NSLog(@"%@", files);
//5.更改到待操作的到目錄下 [fileManager changeCurrentDirectoryPath:[documentDirectorystringByExpandingTildeInPath]]; //建立檔案filenamehttp://www.kmnk03.com/hxpfk/qcd/399.html檔名,contents檔案的內容 NSString *fileName =@"testfilemanager.txt"; NSArray *array = [[NSArrayalloc]initWithObjects:@"hello world",@"hello World", nil];
[fileManagercreateFileAtPath:fileName contents:array attributes:nil];
//6.刪除檔案 [fileManagerremoveItemAtPath:fileName error:nil]; //7.複製檔案後者給前者 NSError *error; [fileManagercopyItemAtPath:test11 toPath:test22 error:&error]; //8.移動檔案 NSString *test33 = [documentDirectory stringByAppendingPathComponent:@"test33.txt"]; if ([fileManager mhttp://www.kmnk03.com/hxpfk/qcd/400.htmloveItemAtPath:test11toPath:test33 error:&error] !=YES) { NSLog(@"111%@", [errorlocalizedDescription]); } //9.檔案是否存在 if ([[NSFileManagerdefaultManaghttp://www.kmnk03.com/hxpfk/qcd/401.htmler]fileExistsAtPath:@"/Users/dlios/Library/Application Support/iPhone Simulator/7.1/Applications/AC7B8788-BDCB-43AC-ABB6-8E9676CF94DC/Documents/testbug/test22333.txt"]) { NSLog(@"yes"); } else NSLog(@"no"); kmnk03.com www.kmnk03.c0m |
相關文章
- iOS開發UI中懶載入的使用方法iOSUI
- iOS 11開發教程(一)iOS
- Flutter外掛iOS端開發教程FlutteriOS
- iOS Touch ID 簡易開發教程iOS
- iOS學習之iOS沙盒(sandbox)機制和檔案操作之NSFileManager(三)iOS
- [iOS開發]Carthage安裝和使用教程iOS
- iOS藍芽4.0開發基礎教程iOS藍芽
- React Native iOS混合開發實戰教程React NativeiOS
- iOS開發教程之手勢識別方法iOS
- axios baseURL 的使用方法iOS
- iOS 11開發教程(三)執行第一個iOS 11程式iOS
- iOS開發視訊教程 UI部分:1、iOS概述和架構iOSUI架構
- iOS 11開發教程(十五)iOS11應用檢視的位置和大小iOS
- 玩轉iOS開發:iOS中的GCD開發(一)iOSGC
- 玩轉iOS開發:iOS中的GCD開發(三)iOSGC
- 玩轉iOS開發:iOS中的GCD開發(二)iOSGC
- ios遊戲開發 Sprite Kit教程:初學者 1iOS遊戲開發
- Xamarin ios 教程 Xamarin跨平臺開發 C#蘋果應用開發iOSC#蘋果
- iOS 11開發教程(五)iOS11模擬器介紹二iOS
- iOS 11開發教程(四)iOS11模擬器介紹一iOS
- iOS 11開發教程(十二)iOS11應用檢視始祖——UIViewiOSUIView
- iOS 11開發教程(十一)瞭解iOS11應用檢視iOS
- iOS 11開發教程(二)編寫第一個iOS 11應用iOS
- 玩轉iOS開發:iOS中的NSOperation開發(一)iOS
- 玩轉iOS開發:iOS中的NSOperation開發(二)iOS
- 有關GPIO的使用方法教程
- IOS 初級開發入門教程(一)介紹篇iOS
- iOS 11開發教程(八)定製iOS11應用程式圖示iOS
- iOS 11開發教程(六)iOS11Main.storyboard檔案編輯介面iOSAI
- iOS 11開發教程(十四)iOS11應用程式碼新增檢視iOS
- iOS開發系列--IOS程式開發概覽iOS
- iOS 開發iOS
- iOS開發iOS
- axios 攔截器 的使用方法iOS
- iOS開發的那些坑iOS
- iOS的鏈式開發iOS
- iOS的元件化開發iOS元件化
- ios的常用開發框架iOS框架