OC基礎(chǔ)—文件管理者

  • iOS中NSFileManager文件常用操作整合

  • 前言:"在Objective-C的編程過程當(dāng)中,常常會(huì)涉及到對(duì)文件的一些操作,OC也提供了專業(yè)的類來進(jìn)行文件操作,那就是NSFileManager類。通過NSFileManager類我們可以對(duì)文件進(jìn)行創(chuàng)建、刪除、移動(dòng)等操作。"

1.NSFileManager

// 獲取電腦桌面的路徑(下面是本機(jī)路徑)
 NSString *desktopPath = @"/Users/hcios/Desktop";
 // 在桌面路徑后拼上想要?jiǎng)?chuàng)建的目錄名(如:test)
 NSString *directoryPath = [desktopPath stringByAppendingPathComponent:@"test"];
 
 // 創(chuàng)建一個(gè)默認(rèn)的fileManager
 NSFileManager *fileManager = [NSFileManager defaultManager];
 
 // fileManager在filePath路徑上創(chuàng)建一個(gè)目錄
 BOOL b = [fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
 NSLog(@"%@", b ? @"創(chuàng)建成功" : @"創(chuàng)建失敗");
 
 // 創(chuàng)建兩個(gè)文件路徑
 NSString *filePath1 = [directoryPath stringByAppendingPathComponent:@"test1.txt"];
 NSString *filePath2 = [directoryPath stringByAppendingPathComponent:@"test2.txt"];
 
 // 創(chuàng)建一個(gè)字符串,轉(zhuǎn)成NSData *類型,寫入文件
 NSString *contents = @"write something to file...";
 NSData *data = [contents dataUsingEncoding:NSUTF8StringEncoding];
 
 // 判斷該路徑文件是否存在
 if (![fileManager fileExistsAtPath:filePath1]) {
 // 文件不存在則創(chuàng)建文件,創(chuàng)建的同時(shí)寫入data
 [fileManager createFileAtPath:filePath1 contents:data attributes:nil];
 }
 if (![fileManager fileExistsAtPath:filePath2]) {
 [fileManager createFileAtPath:filePath2 contents:data attributes:nil];
 }
 
 // 兩種方式獲取一個(gè)目錄中的所有文件名(有時(shí)會(huì)獲取到隱藏文件)
 NSArray *fileArray = [fileManager subpathsAtPath:directoryPath];
 fileArray = [fileManager subpathsOfDirectoryAtPath:directoryPath error:nil];
 NSLog(@"fileArray = %@", fileArray);
 
 // 將directoryPath改為當(dāng)前路徑,fileManager會(huì)默認(rèn)在當(dāng)前路徑下操作
 [fileManager changeCurrentDirectoryPath:directoryPath];
 NSString *filePath3 = @"CurrentDirectoryPath.txt";
 if (![fileManager fileExistsAtPath:filePath3]) {
 [fileManager createFileAtPath:filePath3 contents:data attributes:nil];
 }
 fileArray = [fileManager subpathsAtPath:directoryPath];
 NSLog(@"fileArray = %@", fileArray);
 
 // 刪除文件
 [fileManager removeItemAtPath:filePath3 error:nil];
 fileArray = [fileManager subpathsAtPath:directoryPath];
 NSLog(@"after remove,fileArray = %@", fileArray);
 
 // 在當(dāng)前目錄下創(chuàng)建一個(gè)子目錄sub
 [fileManager createDirectoryAtPath:@"sub" withIntermediateDirectories:YES attributes:nil error:nil];
 fileArray = [fileManager subpathsAtPath:directoryPath];
 NSLog(@"add a sub directory,fileArray = %@", fileArray);
 
 // 將test2.txt移動(dòng)到sub目錄中去
 [fileManager moveItemAtPath:filePath2 toPath:[@"sub" stringByAppendingPathComponent:@"test2.txt"] error:nil];
 fileArray = [fileManager subpathsAtPath:directoryPath];
 NSLog(@"after move,fileArray = %@", fileArray);
 
 
 // 將test1.txt復(fù)制一份到sub目錄中去
 [fileManager copyItemAtPath:filePath1 toPath:[@"sub" stringByAppendingPathComponent:@"test1.txt"] error:nil];
 fileArray = [fileManager subpathsAtPath:directoryPath];
 NSLog(@"after copy,fileArray = %@", fileArray);
 
 // 讀取文件中的內(nèi)容,并將NSData *型數(shù)據(jù)轉(zhuǎn)成NSString *型數(shù)據(jù)
 NSData *getData = [fileManager contentsAtPath:filePath1];
 NSString *getString = [[NSString alloc] initWithData:getData encoding:NSUTF8StringEncoding];
 NSLog(@"getString = %@", getString);```

![圖片](http://upload-images.jianshu.io/upload_images/1429890-cbf2f30e82f976ca.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
***

#2.NSBundle
>NSBundle 類,直接繼承 NSObject 類。 這個(gè)類的對(duì)象,代表了 app 中代碼和資源的文件在文件系統(tǒng)里所在的位置,通俗的說,就是定位了程序使用的資源(代碼,圖形,音樂等數(shù)據(jù))在文件系統(tǒng)里的位置,并可以動(dòng)態(tài)的加載、或卸載掉可執(zhí)行代碼。
bundle在英文中的解釋是“捆、束”的意思,那么我們可以將NSBundle理解為是將程序的所有資源捆在一起的對(duì)象,我們的程序是一個(gè)bundle。 在Finder中,一個(gè)應(yīng)用程序看上去和其他文件沒有什么區(qū)別。但是實(shí)際上它是一個(gè)包含了nib文件,編譯代碼,以及其他資源的目錄. 我們把這個(gè)目錄叫做程序的main bundle,在 Xcode 里,使用應(yīng)用程序、框架、或者插件的時(shí)候,Xcode 會(huì)生成對(duì)應(yīng)的資源的目錄包。
對(duì)于有GUI的應(yīng)用程序來說,我們可以通過NSBundle來獲取資源的路徑,但是對(duì)于沒有GUI的程序(比如OS X的控制臺(tái)程序),就不能通過NSBundle來獲取資源的路徑。
+ (NSBundle *)mainBundle;
上面的mainBundle方法返回一個(gè) NSBundle類的對(duì)象,這個(gè)對(duì)象就是一個(gè)絕對(duì)路徑,這個(gè)路徑保存的是當(dāng)前可執(zhí)行的應(yīng)用程序根目錄路徑,應(yīng)用程序在編譯之后, 資源文件就直接復(fù)制到了根目錄下。然后我們根據(jù)資源文件的名稱和類型就可以獲取到它們,例如下面獲取一張圖片:

// 獲取應(yīng)用程序的main bundle
NSBundle *mainBundle = [NSBundle mainBundle];
// 獲取圖片的路徑(圖片為test.jpg)
NSString *imagePath = [mainBundle pathForResource:@"test" ofType:@"jpg"];
// 根據(jù)圖片的路徑獲取圖片
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
// 將圖片放入imageView中
self.imageView.image = image;

***

# 3.NSURL

// 將網(wǎng)絡(luò)上一張圖片的url用字符串格式保存
NSString *URLString = @"http://7xow65.com1.z0.glb.clouddn.com/wp-content/uploads/2016/03/cefc1e178a82b901adddeaae738da9773912ef3f.jpg";
// 將字符串格式的url轉(zhuǎn)換成OC中 NSURL *類型的對(duì)象
NSURL *url=[NSURL URLWithString:URLString];
// 獲取url地址中的圖片,保存為NSData *類型的對(duì)象
NSData *data = [NSData dataWithContentsOfURL:url];
// 將data轉(zhuǎn)換為UIImage *類型的對(duì)象并放入imageView中
self.imageView.image = [UIImage imageWithData:data];

// 獲取url中各種參數(shù)
NSLog(@"Scheme: %@", [url scheme]);
NSLog(@"Host: %@", [url host]);
NSLog(@"Port: %@", [url port]);
NSLog(@"Path: %@", [url path]);
NSLog(@"Relative path: %@", [url relativePath]);
NSLog(@"Path components as array: %@", [url pathComponents]);
NSLog(@"Parameter string: %@", [url parameterString]);
NSLog(@"Query: %@", [url query]);
NSLog(@"Fragment: %@", [url fragment]);
NSLog(@"User: %@", [url user]);
NSLog(@"Password: %@", [url password]);```


圖片

  • github

| 項(xiàng)目 | 簡介 |
| : | : |
| MGDS_Swif | 逗視視頻直播 |
| MGMiaoBo | 喵播視頻直播 |
| MGDYZB | 斗魚視頻直播 |
| MGDemo | n多小功能合集 |
| MGBaisi | 高度仿寫百思 |
| MGSinaWeibo | 高度仿寫Sina |
| MGLoveFreshBeen | 一款電商App |
| MGWeChat | 小部分實(shí)現(xiàn)微信功能 |
| MGTrasitionPractice | 自定義轉(zhuǎn)場練習(xí) |
| DBFMDemo | 豆瓣電臺(tái) |
| MGPlayer | 一個(gè)播放視頻的Demo |
| MGCollectionView | 環(huán)形圖片排布以及花瓣形排布 |
| MGPuBuLiuDemo | 瀑布流--商品展 |
| MGSlideViewDemo | 一個(gè)簡單點(diǎn)的側(cè)滑效果,仿QQ側(cè)滑 |
| MyResume | 一個(gè)展示自己個(gè)人簡歷的Demo |
| GoodBookDemo | 好書 |

Snip20161026_15.png

Snip20161026_16.png

Snip20161026_35.png
逗視介紹1.gif

逗視介紹2.gif

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 今天就本周作業(yè)的讀取txt文件查找了的一些方法,如下: //讀取文本內(nèi)容NSError *error;NSStri...
    霏誠拜咬o閱讀 743評(píng)論 0 0
  • 在Objective-C的編程過程當(dāng)中,常常會(huì)涉及到對(duì)文件的一些操作,OC也提供了專業(yè)的類來進(jìn)行文件操作,那就是N...
    趙亦晨閱讀 1,497評(píng)論 0 0
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • 框架捆綁 框架是封裝動(dòng)態(tài)共享庫和支持該庫所需的資源文件的分層目錄??蚣鼙鹊湫偷膭?dòng)態(tài)共享庫提供了一些優(yōu)勢,因?yàn)樗鼈優(yōu)?..
    nicedayCoco閱讀 1,798評(píng)論 0 2
  • 218.241.181.202 wxhl60 123456 192.168.10.253 wxhl66 wxhl6...
    CYC666閱讀 1,548評(píng)論 0 6

友情鏈接更多精彩內(nèi)容