• iOS--基础--文件操作


    - 获取应用沙盒根目录
    +(NSString *)YYHomeDirectory{
        return NSHomeDirectory();
    }
    
    //获取Documents目录
    +(NSString *)YYDocumentsDirectory{
        //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        return documentsDirectory;
    }
    
    //获取Tmp目录
    +(NSString *)YYTmpDirectory{
        //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
        NSString *tmpDirectory = NSTemporaryDirectory();
        return tmpDirectory;
    }
    
    //获取Cache目录
    +(NSString *)YYCacheDirectory{
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *cacheDirectory = [paths objectAtIndex:0];
        return cacheDirectory;
    }
    
    //获取Library目录
    +(NSString *)YYLibraryDirectory{
        //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *libraryDirectory = [paths objectAtIndex:0];
        return libraryDirectory;
    }
    
    #pragma mark --Documents下的文件操作
    //得到Documents里的文件路径
    + (NSString *)getFilePathAtDocuments:(NSString *)fileName{
        return [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
    }
    
    //删除Documents里的文件
    + (BOOL)deleteFileAtDocuments:(NSString *)fileName{
        NSString *filePath = [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if(![fileManager fileExistsAtPath:filePath])
        {
            return NO;
        }
        [fileManager removeItemAtPath:filePath error:nil];
        return YES;
    }
    
    //创建指定名字的文件
    + (BOOL)createFileAtDocuments:(NSString *)fileName{
        NSString *filePath = [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if(![fileManager fileExistsAtPath:filePath]){
            [fileManager createFileAtPath:filePath contents:nil attributes:nil];
            return YES;
        }
        return NO;
    }
    
    //创建指定名字的文件夹
    + (BOOL)createDirectoryAtDocuments:(NSString *)fileName{
        NSString *filePath = [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if(![fileManager fileExistsAtPath:filePath]){
            NSError *error = nil;
            [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];
            return YES;
        }
        return NO;
    }
    
    //文件是否存在
    + (BOOL)isFileExistsAtDocuments:(NSString *)fileName{
        NSString *filePath = [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if(![fileManager fileExistsAtPath:filePath]){
            return NO;
        }
        return YES;
    }
    
    //写文件
    +(BOOL)writeFileAtDocumentsWithName:(NSString *) fileName AndContent:(NSString *)content{
    
        NSString *iOSPath = [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
        BOOL isSuccess = [content writeToFile:iOSPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        return isSuccess;
    }
    
    //读文件
    + (NSString*)readFileContentAtDocumentsWithName:(NSString*)fileName{
        NSString *filePath = [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
        NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        return content;
    }
  • 相关阅读:
    在python3中安装mysql扩展,No module named 'ConfigParser'
    Ubuntu安装MySQL和Python库MySQLdb步骤
    python_非阻塞套接字及I/O流
    EFI、UEFI、MBR、GPT的区别
    2018.1.9 博客迁移至csdn
    2017.12.27 sqlSessionFactory和sqlSession(to be continued)
    2017.12.25 Mybatis物理分页插件PageHelper的使用(二)
    2017.12.14 Mybatis物理分页插件PageHelper的使用(一)
    2017.12.12 架构探险-第一章-从一个简单的web应用开始
    2017.12.11 线程池的简单实践
  • 原文地址:https://www.cnblogs.com/howdoudo/p/5586625.html
Copyright © 2020-2023  润新知