美文网首页
NSFileManager文件操作

NSFileManager文件操作

作者: 骑着雅迪小毛驴上班的老瞿 | 来源:发表于2018-01-05 15:29 被阅读0次

// 获取Documents路径

- (NSString *)getDocumentPath{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) firstObject];
    return  path;
}

// 创建文件夹

- (void)createDirectory{
    NSString *documentPath = [self getDocumentPath];
    NSFileManager *filemanager = [NSFileManager defaultManager];
    NSString *iosDirectory = [documentPath stringByAppendingPathComponent:@"ios"];
    BOOL isSuccess = [filemanager createDirectoryAtPath:iosDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    if (isSuccess) {
        NSLog(@"success");
    }else NSLog(@"fail");
    
}

// 创建文件

- (void)createFile{
    NSString *documentsPath = [self getDocumentPath];
    NSFileManager *filemanager = [NSFileManager defaultManager];
    NSString *iospath = [documentsPath stringByAppendingPathComponent:@"ios.txt"];
    BOOL isSuccess = [filemanager createFileAtPath:iospath contents:nil attributes:nil];
    if (isSuccess) {
        NSLog(@"success");
    }else NSLog(@"fail");

}

// 写文件

- (void)writeFile{
    NSString *documentsPath = [self getDocumentPath];
    NSString *iosPath = [documentsPath stringByAppendingPathComponent:@"ios.txt"];
    NSString *content = @"我是一个数据拉";
    BOOL isSuccess = [content writeToFile:iosPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (isSuccess) {
        NSLog(@"success");
    }else{
        NSLog(@"fail");
    }
}

// 读取文件内容

- (void)readFileContent{
    NSString *documentsPath = [self getDocumentPath];
    NSString *iosPath = [documentsPath stringByAppendingPathComponent:@"ios.txt"];
    NSString *content = [NSString stringWithContentsOfFile:iosPath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"read success --%@",content);
    NSUInteger num = [self fileSizeAtPath:iosPath];
    NSLog(@"---%lud",(unsigned long)num);
}

// 判断文件是否存在

- (BOOL)isSxistAtPath:(NSString *)filePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isExist = [fileManager fileExistsAtPath:filePath];
    return isExist;
}

// 计算文件大小

- (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 isExsit = [filemanager fileExistsAtPath:folderPath];
    if (isExsit) {
        NSEnumerator *chileFileEnumerator = [[filemanager subpathsAtPath:folderPath] objectEnumerator];
        unsigned long long folderSize = 0;
        NSString *fileName = @"";
        while ((fileName = [chileFileEnumerator 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 *documentPath = [self getDocumentPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *iosPath = [documentPath stringByAppendingPathComponent:@"ios.txt"];
    BOOL isSuccess = [fileManager removeItemAtPath:iosPath error:nil];
    if (isSuccess) {
        NSLog(@"delete success");
    }else{
        NSLog(@"delete fail");
    }
}

// 移动文件

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

// 重命名

- (void)renameFileName{
    /*
    文件重命名的两个方法:
    方法一、思路:1.提取原有文件data,
    2.将data存入新建的文件,
    3.删除原有文件
    方法二、用[manager moveItemAtPath:filePath toPath:newPath error:nil];方法将filepath
    路径下的文件名换成newPath
    对文件NSData赋值,在文件内将内容更改后,move后的文件内容保持跟最后修改的一致
    如果文件NSdata未赋值,在程序外文件内更改,move后新文件仍为空。
     */
    // 通过移动文件对文件重命名
    NSString *documentsPath = [self getDocumentPath];
    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");
    }
}

// 文件复制

- (void)copyFileName{
    NSString *documentsPath = [self getDocumentPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"ios.txt"];
    NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"rename.txt"];
    if ([fileManager copyItemAtPath:filePath toPath:moveToPath error:nil]) {
        NSLog(@"success");
    }else{
        NSLog(@"fail");
    }
}

相关文章

  • IOS文件管理

    文件操作 NSFileManager 1.NSFileManager 专门负责文件/文件夹的管理操作,包括创建/删...

  • NSFileManager

    NSFileManager 是用来管理文件系统的,常用来操作文件/文件夹NSFileManager使用了单列模式,...

  • ios 文件操作

    NSFileManager NSFileManager允许你对文件目录和文件进行基本操作。以下列出其一些常用方法。...

  • NSFileManager类

    文件操作 基于NSFileManager类,允许用户对文件进行基本操作。这些操作包括:创建新的文件、读取文件、对文...

  • NSFileManager文件操作

    // 获取Documents路径 // 创建文件夹 // 创建文件 // 写文件 // 读取文件内容 // 判断文...

  • 文件操作--NSFileManager

    iOS的沙盒机制,应用只能访问自己应用目录下的文件。默认情况下,每个沙盒含有3个文件夹:Documents, Li...

  • iOS中的NSFileManager

    NSFileManager NSFileManager是用来管理文件系统管理的 它可以用来操作常见文件夹 NSFi...

  • NSFileManager终极杀手

    NSFileManager 想操作文件,该去了解下NSFileManager 注意://小窍门:打印数组或者字典,...

  • Object-C文件管理对文件的操作以及对文件内容的操作

    对文件的操作 一、 文件管理常用的类和方法 1、NSFileManager: 提供了对文件的基本操作类,对文件的删...

  • iOS 文件操作(NSFileManager)

    iOS的沙盒机制,应用只能访问自己应用目录下的文件。iOS不像Android,没有SD卡概念,不能直接访问图像、视...

网友评论

      本文标题:NSFileManager文件操作

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