什么是NSFileManager?
NSFileManager是用來管理文件系統(tǒng)的
它可以用來進(jìn)行常見的文件\文件夾操作(拷貝螟加、剪切坡倔、創(chuàng)建等)NSFileManager使用了單例模式singleton
使用defaultManager方法可以獲得那個(gè)單例對(duì)象
[NSFileManager defaultManager]
//NSFileManager 用于判斷
NSString *filePath = @"/Users/zhaoxiaohu/Desktop/arr.plist";
NSString *filePath2 = @"/";
1) 判斷文件是否存在
//創(chuàng)建文件管理對(duì)象
//調(diào)用defaultManager 創(chuàng)建一個(gè)文件管理的單例對(duì)象
//單例對(duì)象:在程序運(yùn)行期間,只有一個(gè)對(duì)象存在
NSFileManager *fm = [NSFileManager defaultManager];
// YES 存在 NO 不存在
BOOL isYES = [fm fileExistsAtPath:filePath];
NSLog(@"-->%d",isYES);
2) 判斷是否是一個(gè)目錄
if(isYES){
BOOL isDir;
// 2) 判斷是否是一個(gè)目錄
[fm fileExistsAtPath:filePath isDirectory:&isDir];
if (isDir) {
NSLog(@"這是一個(gè)目錄");
}else{
NSLog(@"這不是一個(gè)目錄");
}
}
3) 判斷文件是否可讀
isYES = [fm isReadableFileAtPath:filePath];
4) 是否可寫
isYES = [fm isWritableFileAtPath:filePath2];
5) 是否可刪除
isYES = [fm isDeletableFileAtPath:filePath2];
NSLog(@"-->%d",isYES);
6)獲取文件的信息(屬性)
//創(chuàng)建文件對(duì)象
NSFileManager *fm = [NSFileManager defaultManager];
NSString *filePath = @"/Users/zhaoxiaohu/Desktop/arr.plist";
NSString *dirPath = @"/Users/zhaoxiaohu/Desktop/a";
//1)如何獲取文件的信息(屬性)
NSDictionary *dict = [fm attributesOfItemAtPath:filePath error:nil];
NSLog(@"%@",dict);
NSLog(@"%@,%@",[dict objectForKey:@"NSFileOwnerAccountName"],dict[@"NSFileOwnerAccountName"]);
7)獲取指定目錄下文件及子目錄
//使用遞歸的方式 獲取當(dāng)前目錄及子目錄下的所有的文件及文件夾
NSArray *subPaths = [fm subpathsAtPath:dirPath];
//(推薦使用)subpathsOfDirectoryAtPath 不是使用遞歸的方式獲取的
subPaths = [fm subpathsOfDirectoryAtPath:dirPath error:nil];
NSLog(@"subPaths = %@",subPaths);
8)獲取指定目錄下的文件及目錄信息(不在獲取后代路徑)
subPaths = [fm contentsOfDirectoryAtPath:dirPath error:nil];
NSLog(@"subPaths = %@",subPaths);
9)創(chuàng)建目錄
//創(chuàng)建文件管理對(duì)象
NSFileManager *fm = [NSFileManager defaultManager];
//如何創(chuàng)建目錄 (路徑 :/Users/zhaoxiaohu/Desktop/aaa)
NSString *createDirPath = @"/Users/zhaoxiaohu/Desktop/aaa/ccc/bbb/love.txt";
fm createDirectoryAtPath:@"路徑" withIntermediateDirectories:YES/NO 創(chuàng)建路徑的時(shí)候,YES自動(dòng)創(chuàng)建路徑中缺少的目錄,NO的不會(huì)創(chuàng)建缺少的目錄 attributes:屬性的字典 error:錯(cuò)誤對(duì)象
BOOL isYES = [fm createDirectoryAtPath:createDirPath withIntermediateDirectories:YES attributes:nil error:nil];
if (isYES) {
NSLog(@"成功");
}
10)創(chuàng)建文件
//如何創(chuàng)建文件
NSString *str = @"每當(dāng)我錯(cuò)過一個(gè)女孩,我就向山上放一塊磚,于是就有了長城";
//writeToFile
//fm createFileAtPath:@"路徑" contents: NSData類型的數(shù)據(jù) attributes:文件的屬性的字典
//創(chuàng)建NSData? 是一個(gè)處理二進(jìn)制數(shù)據(jù)的類
//NSString -----> NSData (把NSString轉(zhuǎn)化成NSData)
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
BOOL isYes;
// createFileAtPath 創(chuàng)建文件
isYes = [fm createFileAtPath:createDirPath contents:data attributes:nil];
NSLog(@"isYes = %d",isYes);
11)copy文件
//如何copy文件
NSString *targetPath = @"/Users/zhaoxiaohu/Desktop/aaa/ccc/love.txt";
[fm copyItemAtPath:createDirPath toPath:targetPath error:nil];
NSString *targetPath = @"/Users/zhaoxiaohu/Desktop/aaa/love.txt";
12)移動(dòng)文件
//如何移動(dòng)文件
[fm moveItemAtPath:createDirPath toPath:targetPath error:nil];
13)刪除文件
//如何刪除文件
[fm removeItemAtPath:targetPath error:nil];