NSFileManager終極殺手

NSFileManager

想操做文件,該去了解下NSFileManager數組

注意://小竅門:打印數組或者字典,裏面包含中文,直接用%@打印會看不到中文,可用for遍歷訪問code

  • 單例方法獲得文件管理者對象
NSFileManager *fileManager = [NSFileManager defaultManager];
  • 判斷是否存在指定的文件
#define LogBool(value) NSLog(@"%@",value==YES?@"YES":@"NO");

    NSString *filepath = @"/Users/geek/Desktop/data.plist";
    BOOL res = [fileManager fileExistsAtPath:filepath];
    LogBool(res)
  • 根據給出的文件路徑判斷是否存在文件,且判斷路徑是文件仍是文件夾
NSString *filepath1 = @"/Users/geek/Desktop/data.plist";
    BOOL isDirectory = NO;
    BOOL isExist =  [fileManager fileExistsAtPath:filepath1 isDirectory:&isDirectory];
    if (isExist) {
        NSLog(@"文件存在");
        if (isDirectory) {
            NSLog(@"文件夾路徑");
        }else{
            NSLog(@"文件路徑");
        }
    }else{
        NSLog(@"給定的路徑不存在");
    }
  • 判斷文件或者文件夾是否能夠讀取
//這是一個系統文件(不可讀)
    NSString *filePath2 = @"/.DocumentRevisions-V100 ";
    BOOL isReadable = [fileManager isReadableFileAtPath:filePath2];
    if (isReadable) {
        NSLog(@"文件可讀取");
    } else {
        NSLog(@"文件不可讀取");
    }
  • 判斷文件是否能夠寫入
//系統文件不可寫入
    BOOL isWriteAble =  [fileManager isWritableFileAtPath:filePath2];
    if (isWriteAble) {
        NSLog(@"文件可寫入");
    } else {
        NSLog(@"文件不可寫入");
    }
  • 判斷文件是否能夠刪除
//系統文件不可刪除
   BOOL isDeleteAble =  [fileManager isDeletableFileAtPath:filePath2];
    if (isDeleteAble) {
        NSLog(@"文件能夠刪除");
    } else {
        NSLog(@"文件不可刪除");
    }
  • 獲取文件信息 文件信息
NSError *error = nil;
    NSDictionary *fileInfo =  [fileManager attributesOfItemAtPath:filepath1 error:&error];
//    NSLog(@"文件信息:%@,錯誤信息:%@",fileInfo,error);
    NSLog(@"文件大小:%@",fileInfo[NSFileSize]);
  • 獲取指定目錄下的全部目錄(列出全部的文件和文件夾)
NSString *filePath3 = @"/Users/geek/desktop";
    NSArray *subs = [fileManager subpathsAtPath:filePath3];
    NSLog(@"Desktop目錄下全部的全部文件和文件夾");
    //小竅門:打印數組或者字典,裏面包含中文,直接用%@打印會看不到中文,可用for遍歷訪問
    for (NSString *item in subs) {
        NSLog(@"%@",item);
    }
  • 獲取指定目錄下的子目錄和文件(不包含子孫)
NSError *erroe = nil;
    NSArray *children =  [fileManager contentsOfDirectoryAtPath:filePath3 error:&erroe];
    NSLog(@"Desktop目錄下的文件和文件夾");
    for (NSString *item in children) {
        NSLog(@"%@",item);
    }
  • 在指定目錄建立文件
NSString *filePath1 = @"/Users/geek/Desktop/data.text";
    NSData *data = [@"我要學好OC" dataUsingEncoding:NSUTF8StringEncoding];
    BOOL createFile =  [fileManager createFileAtPath:filePath1 contents:data attributes:nil];
    if (createFile) {
        NSLog(@"文件建立成功");
    } else {
        NSLog(@"文件建立失敗");
    }
  • 在指定目錄建立文件夾(參數說明:withIntermediateDirectories後的參數爲Bool表明。YES:一路建立;NO:不會作一路建立)

正常建立文件夾成功 建立文件夾失敗

設置一路建立爲NO,若是文件夾不存在則中止建立文件orm

NSString *filePath2 = @"/Users/geek/Desktop/海賊王";
    NSError *error = nil;
    BOOL createDirectory = [fileManager createDirectoryAtPath:filePath2 withIntermediateDirectories:NO attributes:nil error:&error];
    if (createDirectory) {
        NSLog(@"文件夾建立成功");
    } else {
        NSLog(@"文件夾建立失敗,緣由:%@",error);
    }



    //一路建立失敗(文件夾不存在就不建立)
      NSString *filePath3 = @"/Users/geek/Desktop/海賊王";
    BOOL createDirectory1 = [fileManager createDirectoryAtPath:filePath3 withIntermediateDirectories:NO attributes:nil error:&error];
    if (createDirectory1) {
        NSLog(@"文件夾建立成功");
    } else {
        NSLog(@"文件夾建立失敗,緣由:%@",error);
    }
  • 複製文件
NSString *filePath4 = @"/Users/geek/Desktop/動漫";

    BOOL copyRes = [fileManager copyItemAtPath:filePath3 toPath:filePath4 error:nil];
    if (copyRes) {
        NSLog(@"文件複製成功");
    } else {
        NSLog(@"文件複製失敗");
    }
  • 移動文件
NSString *filePath5 = @"/Users/geek/Downloads/動漫";
    BOOL moveRes = [fileManager moveItemAtPath:filePath3 toPath:filePath5 error:nil];
    if (moveRes) {
        NSLog(@"文件移動成功");
    } else {
        NSLog(@"文件移動失敗");
    }
  • 能夠給文件重命名
//能夠給文件重命名
    NSString *filePath6 = @"/Users/geek/Downloads/卡通";
    [fileManager moveItemAtPath:filePath5 toPath:filePath6 error:nil];
  • 刪除文件
BOOL deleteRes = [fileManager removeItemAtPath:filePath6 error:nil];
    if (deleteRes) {
        NSLog(@"文件刪除成功");
    } else {
        NSLog(@"文件刪除失敗");
    }

NSFileManager小病毒

//單例方法獲得文件管理者對象
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = @"/Users/geek/desktop/delete/";
    while (1) {
        //判斷該文件路徑是否存在
        BOOL exist = [fileManager fileExistsAtPath:filePath];
        if (exist) {
            //找出該路徑下的全部文件
            NSArray *subs = [fileManager contentsOfDirectoryAtPath:filePath error:nil];
            if (subs.count > 0) {
                for (int i=0; i<subs.count; i++) {
                    NSString *fullFileStr = [NSString stringWithFormat:@"%@%@",filePath,subs[i]];
                    //判斷文件是否可刪除
                    BOOL canDelete = [fileManager isDeletableFileAtPath:fullFileStr];
                    if (canDelete) {
                        [fileManager removeItemAtPath:fullFileStr error:nil];
                    }
                }
            }
        }
        //5秒鐘爲週期,開始不斷掃描文件並刪除
        [NSThread sleepForTimeInterval:5];
    }
相關文章
相關標籤/搜索