zoukankan      html  css  js  c++  java
  • 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;

    }

     

  • 相关阅读:
    Mac sublime text3 安装插件
    趣题记录
    Shadow DOM及自定义标签
    JavaScript 对象部署 Iterator 接口
    JavaScript实现循环链表
    使用JavaScript实现单向链表
    nodejs深入浅出读书笔记(三)
    nodejs深入浅出读书笔记(一)
    nodejs深入浅出读书笔记(二)
    为什么要了解Event loop?(二)
  • 原文地址:https://www.cnblogs.com/hecanlin/p/10752780.html
Copyright © 2011-2022 走看看