-
iOS中NSFileManager文件常用操作整合
前言:"在Objective-C的編程過(guò)程當(dāng)中,常常會(huì)涉及到對(duì)文件的一些操作纠修,OC也提供了專業(yè)的類來(lái)進(jìn)行文件操作视哑,那就是NSFileManager類。通過(guò)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);```

***
#2.NSBundle
>NSBundle 類,直接繼承 NSObject 類簇秒。 這個(gè)類的對(duì)象鱼喉,代表了 app 中代碼和資源的文件在文件系統(tǒng)里所在的位置,通俗的說(shuō)趋观,就是定位了程序使用的資源(代碼扛禽,圖形,音樂(lè)等數(shù)據(jù))在文件系統(tǒng)里的位置皱坛,并可以動(dòng)態(tài)的加載编曼、或卸載掉可執(zhí)行代碼。
bundle在英文中的解釋是“捆剩辟、束”的意思掐场,那么我們可以將NSBundle理解為是將程序的所有資源捆在一起的對(duì)象,我們的程序是一個(gè)bundle贩猎。 在Finder中熊户,一個(gè)應(yīng)用程序看上去和其他文件沒(méi)有什么區(qū)別。但是實(shí)際上它是一個(gè)包含了nib文件,編譯代碼,以及其他資源的目錄. 我們把這個(gè)目錄叫做程序的main bundle融欧,在 Xcode 里敏弃,使用應(yīng)用程序、框架噪馏、或者插件的時(shí)候麦到,Xcode 會(huì)生成對(duì)應(yīng)的資源的目錄包。
對(duì)于有GUI的應(yīng)用程序來(lái)說(shuō)欠肾,我們可以通過(guò)NSBundle來(lái)獲取資源的路徑瓶颠,但是對(duì)于沒(méi)有GUI的程序(比如OS X的控制臺(tái)程序),就不能通過(guò)NSBundle來(lái)獲取資源的路徑刺桃。
+ (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)目 | 簡(jiǎn)介 |
| : | : |
| MGDS_Swif | 逗視視頻直播 |
| MGMiaoBo | 喵播視頻直播 |
| MGDYZB | 斗魚(yú)視頻直播 |
| MGDemo | n多小功能合集 |
| MGBaisi | 高度仿寫百思 |
| MGSinaWeibo | 高度仿寫Sina |
| MGLoveFreshBeen | 一款電商App |
| MGWeChat | 小部分實(shí)現(xiàn)微信功能 |
| MGTrasitionPractice | 自定義轉(zhuǎn)場(chǎng)練習(xí) |
| DBFMDemo | 豆瓣電臺(tái) |
| MGPlayer | 一個(gè)播放視頻的Demo |
| MGCollectionView | 環(huán)形圖片排布以及花瓣形排布 |
| MGPuBuLiuDemo | 瀑布流--商品展 |
| MGSlideViewDemo | 一個(gè)簡(jiǎn)單點(diǎn)的側(cè)滑效果,仿QQ側(cè)滑 |
| MyResume | 一個(gè)展示自己個(gè)人簡(jiǎn)歷的Demo |
| GoodBookDemo | 好書 |