簡介
特點(diǎn)
- 1.NSFileManager是一個單例,相當(dāng)于全局變量.
- 2.NSFileManager用來管理和操作沙盒中的文件和文件夾.
- 3.每個路徑名都是一個NSString對象.
- 4.可對文件或文件夾進(jìn)行創(chuàng)建/刪除/復(fù)制/粘貼等操作.
建議使用場景
管理沙盒中的文件或文件夾.
常用方法

image.png
//
// UIViewController+FileManager.m
// myview
//
// Created by mingzhou wang on 2022/4/30.
//
#import "UIViewController+FileManager.h"
@implementation UIViewController (FileManager)
#pragma mark -- 創(chuàng)建文件的路徑
-(NSString*)createFilePath:(NSString*)fileName{
NSString * filePathString = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
return [NSString stringWithFormat:@"%@/%@",filePathString,fileName];
}
#pragma mark 創(chuàng)建文件
-(void)createFile:(NSString*)fileString{
/**
創(chuàng)建文件
@parm createFileAtPath 文件的路徑
@parm contents 文件創(chuàng)建時寫入的內(nèi)容,可為nil
@parm attributes 文件的屬性,可為nil
*/
BOOL isFile = [[NSFileManager defaultManager] createFileAtPath:[self createFilePath:fileString] contents:nil attributes:nil];
if (!isFile) {
NSLog(@"創(chuàng)建失敗");
return;
}
/**
判斷文件是否存在
*/
[self judgeObjectExistence:[self createFilePath:@"NetWork小賤.plist"]];
}
#pragma mark -- 創(chuàng)建文件夾
-(void)createFolder:(NSString*)folderString{
/**
創(chuàng)建文件夾
@parm path 創(chuàng)建文件夾的路徑
@parm createIntermediates 是否展開文件夾的中間目錄
@parm attributes 是創(chuàng)建文件夾設(shè)置的屬性,可為nil
@parm error 創(chuàng)建文件夾返回的錯誤對象,可為nil
*/
NSError * error = nil;
BOOL isFolder = [[NSFileManager defaultManager] createDirectoryAtPath:[self createFilePath:folderString] withIntermediateDirectories:YES attributes:nil error:&error];
if (!isFolder) {
NSLog(@"createFolder_error:%@",error);
return;
}
/**
判斷文件夾是否存在
*/
[self judgeObjectExistence:[self createFilePath:@"NetWork"]];
}
#pragma mark -- 判斷文件是否存在
-(void)judgeObjectExistence:(NSString*)objectPath{
/**
判斷文或者文件夾
BOOL Existence = [fileManager fileExistsAtPath:objectPath isDirectory:YES];
*/
BOOL isExistence = [[NSFileManager defaultManager] fileExistsAtPath:objectPath];
if (!isExistence) {
NSLog(@"不存在");
return;
}
/**
判斷文件或者文件夾是否可讀
*/
[self judgeObjectReadJurisdiction:objectPath];
/**
判斷文件或者文件夾是否可寫權(quán)限
*/
[self judgeObjectWritJurisdiction:objectPath];
/**
判斷文件是否是可執(zhí)行文件
*/
[self judgeObjectExecutJurisdiction:objectPath];
/**
判斷是否是可刪除的對象
*/
[self judgeObjectDeletJurisdiction:objectPath];
/**
獲取可視化的文件字符串
*/
[self getVisualizationFileString:objectPath];
}
#pragma mark -- 判斷對象的權(quán)限是否可讀
-(void)judgeObjectReadJurisdiction:(NSString*)objectPath{
/**
判斷對象是否有可讀權(quán)限
@parm path 對象路徑
*/
BOOL isRead = [[NSFileManager defaultManager] isReadableFileAtPath:objectPath];
if (!isRead) {
NSLog(@"沒有可讀權(quán)限");
}
}
#pragma mark -- 判斷對象是否有可寫的權(quán)限
-(void)judgeObjectWritJurisdiction:(NSString*)objectPath{
/**
判斷對象是否有可寫權(quán)限
@parm path 對象路徑
*/
BOOL isWrit = [[NSFileManager defaultManager] isWritableFileAtPath:objectPath];
if (!isWrit) {
NSLog(@"沒有可寫權(quán)限");
}
}
#pragma mark -- 判斷是否是可執(zhí)行文件
-(void)judgeObjectExecutJurisdiction:(NSString*)objectPath{
/**
判斷是否是可執(zhí)行文件
@parm path 對象路徑
知識:
可執(zhí)行文件 (executable file) 指的是可以由操作系統(tǒng)進(jìn)行加載執(zhí)行的文件。
*/
BOOL isExecut = [[NSFileManager defaultManager] isExecutableFileAtPath:objectPath];
if (!isExecut) {
NSLog(@"不是可執(zhí)行文件");
}
}
#pragma mark -- 判斷文件或者文件夾是否可以刪除
-(void)judgeObjectDeletJurisdiction:(NSString*)objectPath{
/**
判斷是否是可可刪除的對象
@parm path 對象路徑
*/
BOOL isDelet = [[NSFileManager defaultManager] isDeletableFileAtPath:objectPath];
if (!isDelet) {
NSLog(@"不可刪除的對象");
}
}
#pragma mark 文件數(shù)據(jù)的寫入
-(void)writeToFile:(NSString*)content path:(NSString*)path{
NSData * data = [content dataUsingEncoding:NSUTF8StringEncoding];
BOOL isFinish = [data writeToFile:path atomically:YES];
if (!isFinish) {
NSLog(@"寫入失敗");
}
}
#pragma mark -- 判斷兩個文件內(nèi)容是否相等
-(void)fileContentComparison{
/**
測試文件內(nèi)容是否相等
*/
NSArray * contentArray = @[@"清明時節(jié)雨紛紛",@"路上行人欲斷魂",@"成功QQ吧",@"成功QQ吧"];
NSArray * fileNameArray = @[@"cg_a.txt",@"cg_b.txt",@"cg_c.txt",@"cg_d.txt"];
/**
創(chuàng)建文件,并寫入數(shù)據(jù)
*/
for (unsigned int i =0 ; i<fileNameArray.count; i++) {
BOOL isFile = [[NSFileManager defaultManager] createFileAtPath:[self createFilePath:fileNameArray[i]] contents:contentArray[i] attributes:nil];
if (!isFile) {
NSLog(@"文件創(chuàng)建失敗");
}
}
/**
進(jìn)行分組比較
*/
for (unsigned int i =0; i<fileNameArray.count; i++) {
/**
文件內(nèi)容的比較
@parm path1,path2 文件的路徑
*/
if (i==0||i%2==0) {
NSLog(@"%d",i);
BOOL isIdentical = [[NSFileManager defaultManager] contentsEqualAtPath:[self createFilePath:fileNameArray[i]] andPath:[self createFilePath:fileNameArray[i+1]]];
if (!isIdentical) {
NSLog(@"文件不相同-%@--%@",fileNameArray[i],fileNameArray[i+1]);
}else{
NSLog(@"文件相同-%@--%@",fileNameArray[i],fileNameArray[i+1]);
}
/**
輸出:
2017-03-30 15:08:04.833 NSFileManager[3899:940954] 0
2017-03-30 15:08:04.833 NSFileManager[3899:940954] 文件不相同-cg_a.txt--cg_b.txt
2017-03-30 15:08:04.834 NSFileManager[3899:940954] 2
2017-03-30 15:08:04.834 NSFileManager[3899:940954] 文件相同-cg_c.txt--cg_d.txt
*/
}
}
}
#pragma mark -- 獲取可視化文件字符串
-(void)getVisualizationFileString:(NSString*)path{
NSString * displayNameAtPath = [[NSFileManager defaultManager] displayNameAtPath:path];
NSLog(@"可視化串:%@",displayNameAtPath);
/**
輸出:
2017-03-30 15:28:27.354 NSFileManager[4108:1007542] 可視化串:NetWork小賤.plist
2017-03-30 15:28:27.355 NSFileManager[4108:1007542] 可視化串:NetWork
注意:
該返回的可視化字符串,不可用于其他函數(shù)的參數(shù)。對于本地的文件,將返回合適的字符串。
*/
}
#pragma mark 文件或者文件夾的拷貝
-(void)copyFile{
[self createFolder:@"A"];
[self createFile:@"A/a.txt"];
[self createFolder:@"B"];
[self createFolder:@"B/C"];
NSString * FloderNameStr = [self createFilePath:@"A"];
NSString * fileNameStr = [self createFilePath:@"A/a.txt"];
NSString * fileName = [self createFilePath:@"B/b.txt"];
NSString * FloderName = [self createFilePath:@"C"];
/**
文件夾的復(fù)制
*/
[self copyObject:FloderNameStr toPath:FloderName];
/**
文件的復(fù)制
*/
[self copyObject:fileNameStr toPath:fileName];
/**
輸出:
NSFileManager[5178:1361817] 復(fù)制成功
NSFileManager[5178:1361817] 復(fù)制成功
注意:
文件和文件夾的復(fù)制,復(fù)制文件不能提前比被復(fù)制文件存在。比如:B要復(fù)制A,那B在復(fù)制前不能存在,否則將不成功。
*/
}
-(void)copyObject:(NSString*)path toPath:(NSString*) path1 {
/**
文件夾的拷貝
@parm srcPath 要復(fù)制的文件夾路徑
@parm dstPath 目的文件夾路徑
@parm error 錯誤信息
*/
NSError * error;
BOOL isFolder = [[NSFileManager defaultManager] copyItemAtPath:path toPath:path1 error:&error];
if (!isFolder) {
NSLog(@"文件夾復(fù)制失敗-%@",error);
return;
}
NSLog(@"復(fù)制成功");
}
#pragma mark -- 文件和文件夾的移動
-(void)moveFile{
/**
文件夾的移動
*/
[self createFolder:@"M_1"];
NSString * Folder_M1_Path = [self createFilePath:@"M_1"];
NSString * Folder_M2_Path = [self createFilePath:@"M_2"];
/**
我們要將 M_1 移動到 M_2 里面
*/
[self moveObject:Folder_M1_Path toPath:Folder_M2_Path];
/**
文件的移動
*/
[self createFile:@"m.txt"];
NSString * filePath = [self createFilePath:@"m.txt"];
NSString * Folder_M3_Path = [self createFilePath:@"M_3"];
[self moveObject:filePath toPath:Folder_M3_Path];
/**
輸出:
2017-03-30 17:23:59.848 NSFileManager[5818:1525452] 移動成功!
2017-03-30 17:23:59.849 NSFileManager[5818:1525452] 移動成功!
*/
}
-(void)moveObject:(NSString*)path1 toPath:(NSString*)path2{
/**
對象的移動
@parm srcPath 要移動的對象路徑
@parm dstPath 移動后的對象路徑
@parm error 錯誤信息
*/
NSError * error ;
BOOL isMove = [[NSFileManager defaultManager] moveItemAtPath:path1 toPath:path2 error:&error];
if (isMove) {
NSLog(@"移動成功!");
}
}
#pragma mark 創(chuàng)建文件或文件夾間的硬鏈接
-(void)linkFile{
/**
創(chuàng)建文件夾
*/
[self createFile:@"L_k_1.txt"];
NSString * FolderPath = [self createFilePath:@"L_k_1.txt"];
NSString * FolderLinkPath = [self createFilePath:@"L_k_2.txt"];
/**
創(chuàng)建鏈接
*/
[self linkObject:FolderPath toPath:FolderLinkPath];
/**
輸出:
2017-03-30 17:56:53.320 NSFileManager[6181:1659306] 鏈接成功!
*/
}
-(void)linkObject:(NSString*)path1 toPath:(NSString*)path2{
/**
對象間創(chuàng)建硬鏈接
@parm srcPath 要鏈接的對象路徑
@parm dstPath 鏈接后的對象路徑
@parm error 錯誤信息
*/
NSError * error ;
BOOL isLink = [[NSFileManager defaultManager] linkItemAtPath:path1 toPath:path2 error:&error];
if (isLink) {
NSLog(@"鏈接成功!");
}
[@"my love show" writeToFile:[self createFilePath:@"L_k_2.txt"] atomically:YES encoding:NSUTF8StringEncoding error:NULL];
NSString * FolderLinkPath = [self createFilePath:@"L_k_1.txt"];
NSLog(@"%@",[NSString stringWithContentsOfFile:FolderLinkPath encoding:NSUTF8StringEncoding error:NULL]);
}
#pragma mark -- 刪除文件
-(void)removeFile{
/**
刪除文件夾
*/
[self createFolder:@"Love"];
[self removeObject:[self createFilePath:@"Love"]];
/**
刪除文件
*/
[self createFile:@"remove.txt"];
[self removeObject:[self createFilePath:@"remove.txt"]];
}
-(void)removeObject:(NSString*)path{
/**
檢測文件是否存在
*/
BOOL isExistence = [[NSFileManager defaultManager] fileExistsAtPath:path];
if (isExistence) {
NSError * error ;
BOOL isRemove = [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
if (isRemove) {
NSLog(@"%@",@"刪除成功");
}else{
NSLog(@"刪除失敗--error:%@",error);
}
}else{
NSLog(@"文件不存在,請確認(rèn)路徑");
}
/**
輸出:
2017-03-31 11:28:24.004 NSFileManager[2190:371451] 刪除成功
2017-03-31 11:28:24.007 NSFileManager[2190:371451] 刪除成功
*/
}
#pragma mark -- 獲取數(shù)據(jù)
-(void)getDataFile{
BOOL isFile = [[NSFileManager defaultManager] createFileAtPath:[self createFilePath:@"abc.txt"] contents:[@"成功QQ吧" dataUsingEncoding:NSUTF8StringEncoding ] attributes:nil];
if (isFile) {
[self getData:[self createFilePath:@"abc.txt"]];
}
}
-(void)getData:(NSString*)path{
if (!path) return;
NSData * data = [[NSFileManager defaultManager] contentsAtPath:path];
NSString * content = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"獲取數(shù)據(jù):%@",content);
}
#pragma mark -- 獲取文件或者文件夾的層次結(jié)構(gòu)
-(void)getArrangement{
/**
文件夾層次
*/
[self createFolder:@"ABC/BS/CKK/wwq"];
[self getArrangementObject:[self createFilePath:@"ABC"]];
/**
輸出:
2017-03-31 15:54:00.584 NSFileManager[4859:1110472] 文件的層次:(
BS,
"BS/CKK",
"BS/CKK/wwq"
)
注意:
該方法一版不要調(diào)用,因?yàn)橛?jì)算損耗較大
*/
}
-(void)getArrangementObject:(NSString*)path{
if (!path) return;
NSArray * array = [[NSFileManager defaultManager] subpathsAtPath:path];
NSLog(@"文件的層次:%@",array);
}
#pragma mark -- 獲取文件或者文件夾在系統(tǒng)中代表的字符
-(void)getfileSystemRepresentation{
/**
創(chuàng)建一個文件
*/
[self createFolder:@"SR_Love"];
const char * ar = [[NSFileManager defaultManager] fileSystemRepresentationWithPath:[self createFilePath:@"SR_Love"]];
NSLog(@"字符:%s",ar);
}
#pragma mark -- 獲取文件屬性
-(void)getFileAttribute{
[self createFolder:@"Attribute"];
/**
獲取路徑
*/
NSString * FolderPath = [self createFilePath:@"Attribute"];
NSDictionary * FileAttribute = [[NSFileManager defaultManager] attributesOfItemAtPath:FolderPath error:NULL];
NSLog(@"文件的屬性:%@",FileAttribute);
/**
輸出:
2017-03-31 16:35:43.572 NSFileManager[5283:1231846] 文件的屬性:{
NSFileCreationDate = "2017-03-31 08:35:43 +0000";
NSFileExtensionHidden = 0;
NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = staff;
NSFileModificationDate = "2017-03-31 08:35:43 +0000";
NSFileOwnerAccountID = 501;
NSFilePosixPermissions = 493;
NSFileReferenceCount = 2;
NSFileSize = 68;
NSFileSystemFileNumber = 22010573;
NSFileSystemNumber = 16777218;
NSFileType = NSFileTypeDirectory;
}
介紹:
NSFileCreationDate 文件創(chuàng)建的時間
NSFileExtensionHidden 文件路徑是否展開
NSFileGroupOwnerAccountID 創(chuàng)建分組的ID
NSFileGroupOwnerAccountName 創(chuàng)建分組的名字
NSFileModificationDate 文件修改的時間
NSFileOwnerAccountID 創(chuàng)建者的ID
NSFilePosixPermissions 文件權(quán)限的編號
NSFileReferenceCount 文件參考數(shù)量
NSFileSize 文件大小
NSFileSystemFileNumber 系統(tǒng)文件編號
NSFileSystemNumber 系統(tǒng)編號
NSFileType 文件類型
*/
}
#pragma mark -- NSFileManagerDelegate
/**
文件移動
*/
-(BOOL)fileManager:(NSFileManager *)fileManager shouldRemoveItemAtURL:(NSURL *)URL{
return YES;
}
-(BOOL)fileManager:(NSFileManager *)fileManager shouldRemoveItemAtPath:(NSString *)path{
return YES;
}
/**
文件復(fù)制
*/
-(BOOL)fileManager:(NSFileManager *)fileManager shouldCopyItemAtURL:(NSURL *)srcURL toURL:(NSURL *)dstURL{
return YES;
}
-(BOOL)fileManager:(NSFileManager *)fileManager shouldCopyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath{
return YES;
}
/**
文件的連接
*/
-(BOOL)fileManager:(NSFileManager *)fileManager shouldLinkItemAtURL:(NSURL *)srcURL toURL:(NSURL *)dstURL{
return YES;
}
-(BOOL)fileManager:(NSFileManager *)fileManager shouldLinkItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath{
return YES;
}
/**
文件的移動
*/
-(BOOL)fileManager:(NSFileManager *)fileManager shouldMoveItemAtURL:(NSURL *)srcURL toURL:(NSURL *)dstURL{
return YES;
}
-(BOOL)fileManager:(NSFileManager *)fileManager shouldMoveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath{
return YES;
}
/**
文件移除
*/
-(BOOL)fileManager:(NSFileManager *)fileManager shouldProceedAfterError:(NSError *)error removingItemAtURL:(NSURL *)URL{
return YES;
}
- (BOOL)fileManager:(NSFileManager *)fileManager shouldProceedAfterError:(NSError *)error removingItemAtPath:(NSString *)path{
return YES;
}
/**
文件連接錯誤
*/
- (BOOL)fileManager:(NSFileManager *)fileManager shouldProceedAfterError:(NSError *)error linkingItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath{
return YES;
}
- (BOOL)fileManager:(NSFileManager *)fileManager shouldProceedAfterError:(NSError *)error linkingItemAtURL:(NSURL *)srcURL toURL:(NSURL *)dstURL {
return YES;
}
@end