• iOS开发之常用路径及文件操作方法


    一、常用的路径方法

    1.获取AppName.app 目录路径:

       NSString *path = [[NSBundle mainBundlebundlePath];

    2.获取Documents目录路径的方法:

        NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES).firstObject;

    3.Library/Caches目录路径方法:

        NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectoryNSUserDomainMaskYES).firstObject;

    4.Library/Application Support目录路径方法:

       [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,   NSUserDomainMask, YES) objectAtIndex:0]

    4.tmp目录路径的方法:

        NSString *tmpPaht = NSTemporaryDirectory();

    5.获取沙盒主目录路径    

        NSString *homePaht = NSHomeDirectory();

     

    二、文件操作相关方法

    1、判断文件是否存在

     

    /** 判断文件是否存在*/

    + (BOOL)isExistFileForPath:(NSString *)strFilePath

    {

        if (strFilePath.length < 1) {

            return NO;

        }

        NSFileManager *fileMgr = [NSFileManager defaultManager];

        BOOL bDir = NO;

        BOOL bExist = [fileMgr fileExistsAtPath:strFilePath isDirectory:&bDir];

        if (!bDir && bExist) {

            return YES;

        }

        return NO;

    }

    /** 判断文件夹目录是否存在*/

    + (BOOL)isExistDirectoryForPath:(NSString *)strDirPath

    {

        if (strDirPath.length < 1) {

            return NO;

        }

        NSFileManager *fileMgr = [NSFileManager defaultManager];

        BOOL bDir = NO;

        BOOL bExist = [fileMgr fileExistsAtPath:strDirPath isDirectory:&bDir];

        if (bDir && bExist) {

            return YES;

        }

        return NO;

    }

    2、创建文件夹

    /** 创建文件夹目录*/

    + (BOOL)createDirectoryForPath:(NSString *)strDirPath

    {

        if (strDirPath.length < 1) {

            return NO;

        }

        if ([ECKUtility isExistDirectoryForPath:strDirPath]) {

            return YES;

        }

        NSFileManager *fileMgr = [NSFileManager defaultManager];

        BOOL bResult = [fileMgr createDirectoryAtPath:strDirPath withIntermediateDirectories:YES attributes:nil error:nil];

        return bResult;

    }

    3、删除文件

    /** 删除文件*/

    + (void)deleteFileOrDirectoryAtPath:(NSString *)strPath

    {

        NSFileManager *fileMgr = [NSFileManager defaultManager];

        NSError *error = nil;

        BOOL bResult = [fileMgr removeItemAtPath:strPath error:&error];

    }

    4、移动文件

    /** 移动文件*/

    + (BOOL)moveFileAtPath:(NSString *)originPath toNewPath:(NSString *)newPath

    {

        if (![ECKUtility isExistFileForPath:originPath]) {

            return NO;

        }

        NSFileManager *fileMgr = [NSFileManager defaultManager];

        BOOL bResult = [fileMgr moveItemAtPath:originPath toPath:newPath error:nil];

        return bResult;

    }

     

  • 相关阅读:
    Java里的堆(heap)栈(stack)和方法区(method)
    SpringMVC 的 Controller 返回各种视图的处理方式
    Nginx Open File Cache
    HandlerInterceptor与MethodInterceptor
    Mysql的with rollup分组统计功能(5.1以上版本)
    idea中@data不生效
    java中? extends T 和? super T解析
    java8排序
    spring boot gateway自定义限流
    spring boot添加logging不能启动且不报错
  • 原文地址:https://www.cnblogs.com/hecanlin/p/10752780.html
Copyright © 2020-2023  润新知