美文网首页数据存储
iOS之NSFileManager文件操作

iOS之NSFileManager文件操作

作者: charlotte2018 | 来源:发表于2017-03-19 17:37 被阅读49次

    NSFileManager是个单例类,也是一个文件管理器。可以通过NSFileManager创建文件夹、创建文件、写文件、读文件内容等等基本功能。

    我们在Documents里面进行举例,首先是获取Documents的路径。

     //获取Documents路径
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = [paths objectAtIndex:0];
        NSLog(@"path:%@", path);
    

    创建文件夹

        NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentPath = [documentArray firstObject];
        NSLog(@"documentPath = %@",documentPath);
        
        NSString *filePath = [documentPath stringByAppendingPathComponent:@"ios"];
        
       BOOL isSuccess =  [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
        NSLog(@"%d",isSuccess);
    

    创建文件

        NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentPath = [documentArray firstObject];
        NSLog(@"documentPath = %@",documentPath);
        
        NSString *filePath = [documentPath stringByAppendingPathComponent:@"ios.txt"];
        
        [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
    

    写文件

        NSString *documentsPath =[self getDocumentsPath];
        NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
        NSString *content = @"我要写数据啦";
        BOOL isSuccess = [content writeToFile:iOSPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    

    读取文件内容

        NSString *documentsPath =[self getDocumentsPath];
        NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
        NSString *content = [NSString stringWithContentsOfFile:iOSPath encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"read success: %@",content);
    

    判断文件是否存在

    NSString *filePath = [documentPath stringByAppendingPathComponent:@"ios.txt"];
        
       BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
    

    计算文件大小

    - (unsigned long long)fileSizeAtPath:(NSString *)filePath{
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL isExist = [fileManager fileExistsAtPath:filePath];
        if (isExist){
            unsigned long long fileSize = [[fileManager attributesOfItemAtPath:filePath error:nil] fileSize];
            return fileSize;
        } else {
            NSLog(@"file is not exist");
            return 0;
        }
    }
    

    计算整个文件夹中所有文件大小

    - (unsigned long long)folderSizeAtPath:(NSString*)folderPath{
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL isExist = [fileManager fileExistsAtPath:folderPath];
        if (isExist){
            NSEnumerator *childFileEnumerator = [[fileManager subpathsAtPath:folderPath] objectEnumerator];
            unsigned long long folderSize = 0;
            NSString *fileName = @"";
            while ((fileName = [childFileEnumerator nextObject]) != nil){
                NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
                folderSize += [self fileSizeAtPath:fileAbsolutePath];
            }
            return folderSize / (1024.0 * 1024.0);
        } else {
            NSLog(@"file is not exist");
            return 0;
        }
    }
    

    删除文件

    -(void)deleteFile{
        NSString *documentsPath =[self getDocumentsPath];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
        BOOL isSuccess = [fileManager removeItemAtPath:iOSPath error:nil];
        if (isSuccess) {
            NSLog(@"delete success");
        }else{
            NSLog(@"delete fail");
        }
    }
    

    移动文件

    - (void)moveFileName
    {
        NSString *documentsPath =[self getDocumentsPath];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
        NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"ios/qq"];
        BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil];
        if (isSuccess) {
            NSLog(@"rename success");
        }else{
            NSLog(@"rename fail");
        }
    }
    

    重命名

    - (void)renameFileName
    {
        //通过移动该文件对文件重命名
        NSString *documentsPath =[self getDocumentsPath];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
        NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"rename.txt"];
        BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil];
        if (isSuccess) {
            NSLog(@"rename success");
        }else{
            NSLog(@"rename fail");
        }
    }
    

    相关文章

      网友评论

        本文标题:iOS之NSFileManager文件操作

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