美文网首页
NSFileManager的使用

NSFileManager的使用

作者: 抹茶不加糖 | 来源:发表于2016-04-21 09:16 被阅读65次
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
    
        NSFileManager *fileManager = [NSFileManager defaultManager];
    #pragma mark - 目录的操作
    //    目录
       NSArray *fileArr = [fileManager contentsOfDirectoryAtPath:@"/Users/jessy/Desktop/tab" error:nil];
        NSLog(@"%@",fileArr);
        for (NSString *detail in fileArr) {
            NSLog(@"detail---%@",detail);
        }
    
    //    详细到每个子目录,,
       NSArray *subArr = [fileManager subpathsOfDirectoryAtPath:@"/Users/jessy/Desktop/tab" error:nil];
        NSLog(@"%@",subArr);
        for (NSString *subDetail in subArr) {
            NSLog(@"subDetail---%@",subDetail);
        }
    
    //    创建目录
        BOOL createDir = [fileManager createDirectoryAtPath:@"/Users/jessy/Desktop/tab/23/12" withIntermediateDirectories:YES attributes:nil error:nil];
        [self judgeName:createDir];
    
    #pragma mark - 文件的操作
    
    //   文件的创建与写入
        NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
        NSString *filePath = [path stringByAppendingPathComponent:@"fileTest.txt"];
        NSString *message = @"ceshia";
        if (![fileManager fileExistsAtPath:filePath]) {
            BOOL createFile = [fileManager createFileAtPath:filePath contents:[message dataUsingEncoding:NSUTF8StringEncoding ] attributes:nil];
            [self judgeName:createFile];
        }
    
    
    //    读取文件的内容
        NSData *fileData = [fileManager contentsAtPath:filePath];
        NSString *fileStr = [[NSString alloc]initWithData:fileData encoding:NSUTF8StringEncoding];
        NSLog(@"%@",fileStr);
    
    //    移动文件
        NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
        NSString *newPath = [cachePath stringByAppendingPathComponent:@"fileTest.txt"];
        if (![fileManager fileExistsAtPath:newPath]) {
            BOOL movePath = [fileManager moveItemAtPath:filePath toPath:newPath error:nil];
            [self judgeName:movePath];
        }
    
    
    //    复制文件
        if (![fileManager fileExistsAtPath:filePath]) {
            BOOL copyFile = [fileManager copyItemAtPath:newPath toPath:filePath error:nil];
            [self judgeName:copyFile];
        }
    
    //    比较两个文件的内容
       BOOL result = [fileManager contentsEqualAtPath:newPath andPath:filePath];
        if (result) {
            NSLog(@"一样");
        }else
        {
            NSLog(@"NO");
        }
    
    //    删除文件
       BOOL removeFile = [fileManager removeItemAtPath:filePath error:nil];
        [self judgeName:removeFile];
    
    
    }
    
    
    - (void)judgeName:(BOOL)name{
        if (name) {
            NSLog(@"成功");
        }else{
            NSLog(@"失败");
        }
    }
    
    
    

    相关文章

      网友评论

          本文标题:NSFileManager的使用

          本文链接:https://www.haomeiwen.com/subject/xgrtrttx.html