zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    JavaScript按纯数字排序
    用jQuery监听浏览器窗口的变化
    jquery-jtemplates.js模板应用
    art-template模板应用
    JavaScript判断当前手机是Android还是iOS系统
    JavaScript数组转字符串,字符串转数组
    JavaScript数字转字符串,字符串转数字
    Play framework 安装
    JQuery判断数组中是否包含某个字符串
    js获取页面宽度高度及屏幕分辨率
  • 原文地址:https://www.cnblogs.com/howdoudo/p/5586625.html
Copyright © 2011-2022 走看看