zoukankan      html  css  js  c++  java
  • 文件管理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);

        

    }

  • 相关阅读:
    OpenJudge 2753 菲波那契数列
    Jmeter的介绍、安装及汉化
    spring集成Java性能监控调优工具-Javamelody
    js-xlsx + handsontable + echarts实现excel上传编辑然后显示成图表
    spring mybatis设置SQL语句打印
    Ribbon负载均衡策略详解
    JVM内存结构、Java内存模型以及Java对象模型之间的区别
    Java的自动拆装箱
    elasticsearch集群管理指南
    ElasticSearch最全分词器比较及使用方法
  • 原文地址:https://www.cnblogs.com/daxueshan/p/6954106.html
Copyright © 2011-2022 走看看