• Object-C-NSFileManager


    +(NSFileManager *)defaultManager;//获得文件管理对象

    -(BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;//创建文件

    -(BOOL)fileExistsAtPath:(NSString *)path;//判断一个文件是否存在

    -(BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory;

    -(BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error;文件复制

    -(BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError**)error;文件移动

    -(BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error;/文件删除

     // 如何进行浅度遍历
        NSString *dirPath=@"/Users/administrator/Desktop/---";
        NSFileManager *fm= [NSFileManager defaultManager];
        NSArray *arr = [fm contentsOfDirectoryAtPath:dirPath error:nil];
        NSLog(@"%@",arr);
        
        //如何进行深度遍历
        NSString *dirPath=@"/Users/administrator/Desktop/---";
        NSFileManager *fm= [NSFileManager defaultManager];
       NSDirectoryEnumerator *dirs=[fm enumeratorAtPath:dirPath];
        NSString *path=[dirs nextObject];
        while(path!=nil)
        {
            NSLog(@"%@",path);
            path=[dirs nextObject];
        }
        //创建一个NSFileManager对象
        NSFileManager *fm=[NSFileManager defaultManager];
        NSString *path=@"/Users/administrator/Desktop/1.txt";
        
        NSString *str=@"这是我创建的一个文件";
        // 如何将字符串类型转化为NSdata;
        NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
        BOOL flag=[fm createFileAtPath:path contents:data attributes:nil];
        if(flag){
         //success;
            //读取文件内容,转化为字符串
            NSString *fileContent=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
            NSLog(@"%@",fileContent);
        }else{
          //fail
        }

  • 相关阅读:
    shell (check return of each line)(PIPESTATUS[@])and sudoer
    mysql slow query---pt-query-digest----db structure consistency,monitor table records before and after transaction.
    http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/index.html
    PHP数据过滤
    PHP二维数组排序函数
    LBS配置
    jQuery+Ajax+PHP+Mysql实现分页显示数据
    流量高,资费贵。且用且珍惜
    jQuery实现的全选、反选和不选功能
    jquery 点点滴滴小记
  • 原文地址:https://www.cnblogs.com/Opaser/p/4563367.html
Copyright © 2020-2023  润新知