NSFileManger

来源:互联网 发布:php视频上传网站源码 编辑:程序博客网 时间:2024/06/06 07:14


//  NSFileManger


//


#import <Foundation/Foundation.h>


int main(int argc, const char * argv[]) {

    @autoreleasepool {

        

        NSError *error = nil;

        

#pragma mark - 创建文件管理器单例对象

        NSFileManager *fileManger = [NSFileManager defaultManager];

    

#pragma mark - 文件遍历

#pragma mark -浅度遍历当前目录下的文件

        NSString *path = @"/Users/dtg/Desktop/file";

        /*

         contentsOfDirectoryAtPath:要遍历的路径

         error:错误信息

         */

        NSArray *array = [[NSFileManager defaultManagercontentsOfDirectoryAtPath:path error:&error];

        NSLog(@"%@",array);

        

#pragma mark -深度遍历

        array = [fileManger subpathsOfDirectoryAtPath:path error:nil];

        //NSLog(@"%@",array);

        

#pragma mark - 判断文件是否存在

        path = @"/Users/dtg/Desktop/file/CocoaPods搭建.rtf";

        /*

         判断文件是否存在的时候一定要加后缀。

         */

        BOOL isExist = [fileManger fileExistsAtPath:path];

        

        if (isExist)

        {

            NSLog(@"存在");

        }

        else

        {

            NSLog(@"不存在");

        }

        

#pragma mark - 创建文件

        path = @"/Users/dtg/Desktop/file/CocoaPods搭建2222.txt";

        

        //文件的默认内容

        NSString *defaultContent = @"hello 小明";

        //NSData 二进制数据

        //把字符串转化为NSData

        NSData *data = [defaultContent dataUsingEncoding:NSUTF8StringEncoding];

        

        /*

         createFileAtPath:要创建文件的路径

         contents:文件内容(NSData类型)

         attributes:文件属性,一般为nil,nil表示采用默认属性。

         */

        //注意:如果文件已经存在会覆盖

        BOOL createOK = [fileManger createFileAtPath:path contents:data attributes:nil];

        if (createOK)

        {

            NSLog(@"创建成功");

        }

        else

        {

            NSLog(@"创建失败");

        }

        

#pragma mark - 创建目录

        path = @"/Users/dtg/Desktop/file/55/55/55";

        /*

         createDirectoryAtPath:要创建的目录,如果文件夹已经存在不会覆盖

         withIntermediateDirectories:是否有中间目录

         attributes:文件夹的属性,nil表示默认属性

         error:错误信息

         */

        createOK = [fileManger createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];

        

        if (createOK)

        {

            NSLog(@"创建目录成功");

        }

        else

        {

            NSLog(@"创建目录失败");

        }

        

        //NSLog(@"error:%@",error);

#pragma mark - 拷贝文件/目录

        //目录1

        NSString *fromDirPath = @"/Users/dtg/Desktop/file/55";

        NSString *toDirPath = @"/Users/dtg/Desktop/file/day10/555";

        

        NSString *fromFilePath = @"/Users/dtg/Desktop/file/CocoaPods搭建2222.txt";

        NSString *toFilePath = @"/Users/dtg/Desktop/file/day10/CocoaPods搭建2222.txt";

#if 0

        //目录与目录拷贝

        [fileManger copyItemAtPath:fromFilePath toPath:toDirPath error:&error];

#endif

        //文件与文件拷贝

        [fileManger copyItemAtPath:fromFilePath toPath:toFilePath error:&error];

        

        NSLog(@"error:%@",error);

        

#pragma mark - 移动文件/目录

        path = @"/Users/dtg/Desktop/file/1";

        NSString *toPath = @"/Users/dtg/Desktop/file/day10/test";

        

        [fileManger moveItemAtPath:path toPath:toPath error:nil];

        

#pragma mark - 删除文件/目录

        toPath = @"/Users/dtg/Desktop/file/day10/test";

        [fileManger removeItemAtPath:toPath error:nil];

        

#pragma mark - 获取文件属性

        path = @"/Users/dtg/Desktop/file/CocoaPods搭建.txt";

        NSDictionary *attributes = [fileManger attributesOfItemAtPath:path error:nil];

        NSLog(@"文件的属性:%@",attributes);

        

        NSLog(@"文件创建时间:%@",attributes[NSFileCreationDate]);

        //获取文件大小

        NSLog(@"%@",attributes[NSFileSize]);

        

        if ([attributes[NSFileSizeisKindOfClass:[NSNumber class]])

        {

            NSLog(@"YES");

        }

        

       

        

        

    }

    return 0;

}


0 0