• 文件管理NSFileManager


    //NSFileManager

     

     

    - (void)viewDidLoad {

        [super viewDidLoad];

     

        

        NSLog(@"%@",NSHomeDirectory());

        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

        NSString *filePath = [docPath stringByAppendingPathComponent:@"firstFM"];

        

        NSFileManager *fm = [NSFileManager defaultManager];

        //.创建

        //1.创建文件夹

        if (![fm fileExistsAtPath:filePath]) {

            NSError *error = nil;

            BOOL isSuc = [fm createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];

            if (!isSuc) {

                NSLog(@"fail:%@",error.localizedDescription);

            }

        }else{

            NSLog(@"file existed");

        }

        //2.创建文件

        NSString *txtPath = [filePath stringByAppendingPathComponent:@"firstTxt.txt"];

        NSString *test    = @"hello world";

        NSData  *writeData= [test dataUsingEncoding:NSUTF8StringEncoding];

        

        if (![fm fileExistsAtPath:txtPath]) {

            BOOL isSuc = [fm createFileAtPath:txtPath contents:writeData attributes:nil];

            if (!isSuc) {

                NSLog(@"fail");

            }

        }else{

            NSLog(@"txt existed");

        }

       

        //1.

    //    [self scanDirectoryOrFile:filePath];

        //2.

        NSString *sourcePath = txtPath;

        NSString *newPath = [docPath stringByAppendingPathComponent:@"firstTxt.txt"];

        

    //    [self moveDiectoryOrFile:sourcePath toNewPath:newPath];

    //    [self deleteDirectoryOrFile:[docPath stringByAppendingPathComponent:@"rrr"]];

        

        [self attributeForDirectoryOrFile:newPath];

      

    }

     //.操作

    //1.遍历文件夹(目录)

    - (void)scanDirectoryOrFile:(NSString *)path{

        NSFileManager *fm = [NSFileManager defaultManager];

        //浅层遍历(只包含当前路径的子目录)

        if ([fm fileExistsAtPath:path]) {

            //数组包含的是文件夹的名字

            NSArray *arr1 = [fm contentsOfDirectoryAtPath:path error:nil];

            NSLog(@"%ld--%@",arr1.count,arr1.firstObject);

     

        }

        //深层遍历(包含路径下所有子文件)

    //    if ([fm fileExistsAtPath:path]) {

    //        NSArray *arrAll = [fm subpathsOfDirectoryAtPath:path error:nil];

    //    }

        

    }

     

    //2.拷贝

    - (void)copyDirectoryOrFile:(NSString *)sourcePath toNewPath:(NSString *)newPath{

        NSFileManager *fm = [NSFileManager defaultManager];

        NSError *error = nil;

        BOOL isSuc = [fm copyItemAtPath:sourcePath toPath:newPath error:&error];

        if (isSuc) {

            NSLog(@"success");

        }else{

            NSLog(@"%@",error.localizedDescription);

        }

        

        

    }

    //3.移动(剪切)

    - (void)moveDiectoryOrFile:(NSString *)sourcePath toNewPath:(NSString *)newPath{

        NSFileManager *fm = [NSFileManager defaultManager];

        NSError *error = nil;

        BOOL isSuc = [fm moveItemAtPath:sourcePath toPath:newPath error:&error];

        if (isSuc) {

            NSLog(@"success");

        }else{

            NSLog(@"%@",error.localizedDescription);

        }

        

    }

     

    //4.删除

    - (void)deleteDirectoryOrFile:(NSString *)path{

        NSFileManager *fm = [NSFileManager defaultManager];

        NSError *error = nil;

        BOOL isSuc = [fm removeItemAtPath:path error:nil];

        if (isSuc) {

            NSLog(@"success");

        }else{

            NSLog(@"%@",error.localizedDescription);

        }

        

        

        

    }

    //.获取文件属性

    - (void)attributeForDirectoryOrFile:(NSString *)path{

        NSFileManager *fm = [NSFileManager defaultManager];

        NSError *error = nil;

        NSDictionary *dic = [fm attributesOfItemAtPath:path error:&error];

        

        NSLog(@"%@",dic);

        

    }

  • 相关阅读:
    python学习:字符编码与转码
    python学习:文件操作
    python学习:基本运算符
    python学习:列表、元组、字典、集合
    python学习:基础知识
    linux常用命令
    hadoop手动安全模式
    System.getProperty("user.dir")的理解
    如何获取SpringBoot项目的applicationContext对象
    spring无法注入bean
  • 原文地址:https://www.cnblogs.com/daxueshan/p/6954106.html
Copyright © 2020-2023  润新知