美文网首页iOS学习笔记
iOS 下文件操作的一些简易封装

iOS 下文件操作的一些简易封装

作者: ZYiDa | 来源:发表于2018-04-28 17:09 被阅读49次
    #import <Foundation/Foundation.h>
    
    @interface NSString (FileManager)
    /*
     *获取沙盒根目录
     */
    + (NSString *)getHomeDirectory;
    
    /*
     *获取 Document 文件夹路径
     */
    + (NSString *)getDocumentsPath;
    
    /*
     *获取 Library 文件夹路径
     */
    + (NSString *)getLibraryPath;
    
    /*
     *获取 Library 文件夹下 Cache 文件夹的路径
     */
    + (NSString *)getLibraryCachesPath;
    
    /*
     *获取 Library 文件夹下 Preference 文件夹的路径
     */
    + (NSString *)getLibraryPreferencePath;
    
    /*
     *获取 tmp 临时文件夹的路径
     */
    + (NSString *)getTmpPath;
    
    /*
     *获取一个目录下所有的文件名
     */
    + (NSArray *)getAllFileNamesAtPath:(NSString *)path;
    
    /*
     *创建一个新的文件夹
     */
    + (NSString *)createNewDirectoryWithPath:(NSString *)path andName:(NSString *)name;
    
    
    /*
     *在目录下写入文件
     */
    + (BOOL)writeFileToPath:(NSString *)path withFileName:(NSString *)fileNmame andFileContent:(NSString *)fileContent;
    
    /*
     *移除一个文件/文件夹
     */
    + (BOOL)removeItemAtPath:(NSString *)path;
    @end
    
    #import "NSString+FileManager.h"
    
    @implementation NSString (FileManager)
    
    
    #pragma mark --
    + (NSArray *)getPathsWithDirectoryType:(NSSearchPathDirectory)type{
        return NSSearchPathForDirectoriesInDomains(type, NSUserDomainMask, YES);
    }
    
    #pragma mark -- 获取沙盒根目录
    + (NSString *)getHomeDirectory{
        return NSHomeDirectory();
    }
    
    #pragma mark -- 获取 Documents 路径
    + (NSString *)getDocumentsPath{
        NSArray *paths = [self getPathsWithDirectoryType:NSDocumentDirectory];
        return [paths objectAtIndex:0];
    }
    
    #pragma mark -- 获取 Library 路径
    + (NSString *)getLibraryPath{
        NSArray *paths = [self getPathsWithDirectoryType:NSLibraryDirectory];
        return [paths objectAtIndex:0];
    }
    
    #pragma mark -- 获取 Library 文件夹下 Cache 文件夹的路径
    +(NSString *)getLibraryCachesPath{
        NSArray *paths = [self getPathsWithDirectoryType:NSCachesDirectory];
        return [paths objectAtIndex:0];
    }
    
    #pragma mark -- 获取 Library 文件夹下 Preference 文件夹的路径
    + (NSString *)getLibraryPreferencePath{
        NSArray *paths = [self getPathsWithDirectoryType:NSPreferencePanesDirectory];
        return [paths objectAtIndex:0];
    }
    
    #pragma mark -- 获取 tmp 临时文件夹的路径
    + (NSString *)getTmpPath{
        return NSTemporaryDirectory();
    }
    
    #pragma mark -- NSFileManager
    + (NSFileManager *)fileManager{
        NSFileManager *manager = [NSFileManager defaultManager];
        return manager;
    }
    
    #pragma mark -- 获取一个目录下所有的文件名
    + (NSArray *)getAllFileNamesAtPath:(NSString *)path{
        NSFileManager *manager = [self fileManager];
        return [manager subpathsAtPath:path];
    }
    
    #pragma mark -- 创建一个新的文件夹
    + (NSString *)createNewDirectoryWithPath:(NSString *)path andName:(NSString *)name{
        NSFileManager *manager = [self fileManager];
        NSString *newDirectoryPath = [path stringByAppendingPathComponent:name];
        NSError *error;
        BOOL isDir = NO;
        BOOL isExist = [manager fileExistsAtPath:newDirectoryPath isDirectory:&isDir];
        if (!isDir && !isExist) {
            BOOL isSuccess = [manager createDirectoryAtPath:newDirectoryPath withIntermediateDirectories:YES attributes:nil error:&error];
            return isSuccess ? newDirectoryPath:@"路径创建失败";
        }
        /*
         *路径创建失败的可能原因有:1-没有权限;2-路径已存在;3-名称错误;4-其它
         */
        return @"路径创建失败";
    }
    
    #pragma mark -- 在目录下写入文件
    + (BOOL)writeFileToPath:(NSString *)path withFileName:(NSString *)fileNmame andFileContent:(NSString *)fileContent{
        NSString *filePath = [path stringByAppendingPathComponent:@"/xxx.txt"];
        BOOL isWriteSuccess = [fileContent writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        return isWriteSuccess;
    }
    
    #pragma mark -- 移除一个文件/文件夹
    +(BOOL)removeItemAtPath:(NSString *)path{
        NSFileManager *maanager = [self fileManager];
        NSError *error;
        BOOL ret = [maanager removeItemAtPath:path error:&error];
        if (!ret) {
            NSLog(@"**** Failed:%@ ****",error);
        }
        return ret;
    }
    
    @end
    

    这是我整理的笔记,各位有需要的可以直接拿去用。不足的地方还请 多多指教,谢谢了。

    相关文章

      网友评论

        本文标题:iOS 下文件操作的一些简易封装

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