• 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;
    }
  • 相关阅读:
    锋利的Jquery 读书有感(三)
    锋利的Jquery 读书有感(二)
    锋利的Jquery 读书有感
    读head first servlet and jsp有感(3)(转)
    读head first servlet and jsp有感(2)(转)
    读head first servlet and jsp有感(1)(转)
    JSTL标签与EL表达式之间的微妙关系
    JDBC学习
    Mysql学习(二)
    JAVA相关问题整理(四)
  • 原文地址:https://www.cnblogs.com/howdoudo/p/5586625.html
Copyright © 2020-2023  润新知