文件管理iOS类FileManager

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
FileManager.h文件:

#import <Foundation/Foundation.h>


@interface FileManager : NSObject

+(BOOL)fileExistsAtPath:(NSString *)path;

+(NSString *)getDocumentsPath;

+(BOOL)createDirectoryWithPath:(NSString *)path;

+(BOOL)removeFileAtPath:(NSString *)filePath;

+(BOOL)renameFileName:(NSString *)oldName toNewName:(NSString *)newName;

+(NSData *)readFileContent:(NSString *)filePath;

+(BOOL)saveToDirectory:(NSString *)path data:(NSData *)data name:(NSString *)newName;

+(float)getFileSize:(NSString *)filePath;

@end

FileManager.m文件:

#import "FileManager.h"

@class NSFileManager;

#define DocumentsPath [self getDocumentsPath]

@implementation FileManager


//判断目录是否存在

+(BOOL)fileExistsAtPath:(NSString *)path

{

    return [[NSFileManager defaultManager] fileExistsAtPath:path];

}


//获取Documents路径

+(NSString *)getDocumentsPath

{

    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString * documentsDirectory = [paths objectAtIndex:0];

    return documentsDirectory;

}


//在Documents目录下创建新目录或文件

+(BOOL)createDirectoryWithPath:(NSString *)path

{

    BOOL result = NO;

    NSFileManager * fileManager = [NSFileManager defaultManager];

    NSString * myDirectory = [DocumentsPath stringByAppendingPathComponent:path];

    if ([fileManager fileExistsAtPath:myDirectory] == NO) {

        NSError * error = nil;

                result=[fileManager createDirectoryAtPath:myDirectory withIntermediateDirectories:NO attributes:nil error:&error];

        if (error) {

            NSLog(@"创建目录失败:%@",[error localizedDescription]);

        }


    }

    return result;

}


//删除目录或文件

+(BOOL)removeFileAtPath:(NSString *)filePath

{

    BOOL result = NO;

    NSError * error = nil;

    NSString * fullPath = [DocumentsPath stringByAppendingPathComponent:filePath];

    result = [[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error];

    if (error)

    {

        NSLog(@"删除失败:%@",[error localizedDescription]);

    }

    return result;

}


//重命名目录或文件

+(BOOL)renameFileName:(NSString *)oldName toNewName:(NSString *)newName

{

    BOOL result = NO;

    NSError * error = nil;

    result = [[NSFileManager defaultManager] moveItemAtPath:[DocumentsPath stringByAppendingPathComponent:oldName] toPath:[DocumentsPath stringByAppendingPathComponent:newName] error:&error];

    if (error) {

        NSLog(@"重命名失败:%@",[error localizedDescription]);

    }

    return result;

}


//读取某个文件

+(NSData *)readFileContent:(NSString *)filePath

{

    return [[NSFileManager defaultManager] contentsAtPath:[DocumentsPath stringByAppendingPathComponent:filePath]];

}


//保存某个文件

+(BOOL)saveToDirectory:(NSString *)path data:(NSData *)data name:(NSString *)newName

{

    return [[NSFileManager defaultManager] createFileAtPath:[DocumentsPath stringByAppendingPathComponent:[path stringByAppendingPathComponent:newName]] contents:data attributes:nil];

  

}


//计算文件大小

+(float)getFileSize:(NSString *)filePath

{

    NSError * error = nil;

    NSDictionary * attributes = [NSDictionary dictionaryWithDictionary:[[NSFileManager defaultManager] attributesOfItemAtPath:[DocumentsPath stringByAppendingPathComponent:filePath] error:&error]];

    if (attributes != nil)

    {

        return [[attributes objectForKey:NSFileSize] floatValue];

    }

    if (error) {

        NSLog(@"计算文件大小失败:%@",[error localizedDescription]);

    }

    return 0;

}

@end


标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇: .NET实现注册发送邮件激活账户

下一篇:汉字转拼音源码(C#)