日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IOS--文件管理NSFileManager

發布時間:2023/12/2 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS--文件管理NSFileManager 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
iOS的沙盒機制。應用僅僅能訪問自己應用文件夾下的文件。iOS不像android。沒有SD 卡概念。不能直接訪問圖像、視頻等內容。

iOS應用產生的內容,如圖像、文件、緩存內容等都必須存儲在自己的沙盒內。默認情況下,每一個沙盒含有3個文件 夾:Documents, Library 和 tmp。

Library包括Caches、Preferences文件夾。 Documents:蘋果建議將程序創建產生的文件以及應用瀏覽產生的文件數據保存在該文件夾下,iTunes備份和恢復的時候會包括此文件夾 Library:存儲程序的默認設置或其他狀態信息; Library/Caches:存放緩存文件,保存應用的持久化數據。用于應用升級或者應用關閉后的數據保存,不會被itunes同步,所以為了降低同步的時間,能夠考慮將一些比較大的文件而又不須要備份的文件放到這個文件夾下。 tmp:提供一個即時創建暫時文件的地方,但不須要持久化。在應用關閉后,該文件夾下的數據將刪除。也可能系統在程序不執行的時候清除。 a:獲取應用沙盒根路徑: -(void)dirHome{ NSString *dirHome=NSHomeDirectory(); NSLog(@"app_home: %@",dirHome); } b:獲取Documents文件夾路徑: -(NSString *)dirDoc{ //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSLog(@"app_home_doc: %@",documentsDirectory); return documentsDirectory; } c:獲取Library文件夾路徑: -(void)dirLib{ //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryDirectory = [paths objectAtIndex:0]; NSLog(@"app_home_lib: %@",libraryDirectory); } d:獲取Cache文件夾路徑: -(void)dirCache{ NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachePath = [cacPath objectAtIndex:0]; NSLog(@"app_home_lib_cache: %@",cachePath); } e:獲取Tmp文件夾路徑: -(void)dirTmp{ //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"]; NSString *tmpDirectory = NSTemporaryDirectory(); NSLog(@"app_home_tmp: %@",tmpDirectory); } f:創建文件夾: -(void *)createDir{ NSString *documentsPath =[self dirDoc]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; // 創建文件夾 BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil]; if (res) { NSLog(@"文件夾創建成功"); }else NSLog(@"文件夾創建失敗"); } g:創建文件 -(void *)createFile{ NSString *documentsPath =[self dirDoc]; NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil]; if (res) { NSLog(@"文件創建成功: %@" ,testPath); }else NSLog(@"文件創建失敗"); } h:寫數據到文件: -(void)writeFile{ NSString *documentsPath =[self dirDoc]; NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; NSString *content=@"測試寫入內容!"; BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; if (res) { NSLog(@"文件寫入成功"); }else NSLog(@"文件寫入失敗"); } i:讀文件數據: -(void)readFile{ NSString *documentsPath =[self dirDoc]; NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; // NSData *data = [NSData dataWithContentsOfFile:testPath]; // NSLog(@"文件讀取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil]; NSLog(@"文件讀取成功: %@",content); } j:文件屬性: -(void)fileAttriutes{ NSString *documentsPath =[self dirDoc]; NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil]; NSArray *keys; id key, value; keys = [fileAttributes allKeys]; int count = [keys count]; for (int i = 0; i < count; i++) { key = [keys objectAtIndex: i]; value = [fileAttributes objectForKey: key]; NSLog (@"Key: %@ for value: %@", key, value); } } k:刪除文件: -(void)deleteFile{ NSString *documentsPath =[self dirDoc]; NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"]; BOOL res=[fileManager removeItemAtPath:testPath error:nil]; if (res) { NSLog(@"文件刪除成功"); }else NSLog(@"文件刪除失敗"); NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO"); }


轉載于:https://www.cnblogs.com/blfbuaa/p/6940502.html

總結

以上是生活随笔為你收集整理的IOS--文件管理NSFileManager的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。