NSFileManager

此篇是关于NSFileManager如何使用.包括创建文件,目录.删除文件等操作

获取Documents路径

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

在Documents里创建目录

NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSLog(@"%@",filePath); NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *fooDirectory = [filePath stringByAppendingPathComponent:@"foo"]; [fileManager createDirectoryAtPath:fooDirectory withIntermediateDirectories:YES attributes:nil error:nil];

在foo目录中创建文件,并写入内容

`
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"%@",filePath);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *fooDirectory = [filePath stringByAppendingPathComponent:@"fuck"];
[fileManager createDirectoryAtPath:fooDirectory withIntermediateDirectories:YES attributes:nil error:nil];
NSString *filePathOftest = [fooDirectory stringByAppendingPathComponent:@"haha001.txt"];

NSString *str = @"write something";
[fileManager createFileAtPath:filePathOftest contents:[str dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

`

获取文件夹里面所有文件名

NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSLog(@"%@",filePath); NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *fooDirectory = [filePath stringByAppendingPathComponent:@"fuck"]; NSArray *files = [fileManage subpathsAtPath: fooDirectory ];

判断文件是否存在

`

  • (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 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:@"foo.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:@"foo.txt"]; NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"foof.txt"]; 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:@"foo.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"); } }

你可能感兴趣的:(NSFileManager)