iOS文件操作

作者: 奔跑2015蜗牛 | 来源:发表于2015-09-18 15:26 被阅读496次

    一直在倒腾iOS逆向,逆向水平受限于正向水平。边学iOS开发边学逆向,感觉这个过程有点艰辛。平时写代码写得不是很多,然后每次需要用iOS文件操作的时候,总是需要百度Google。因此,想封装一个iOS文件操作工具类,不知道自己这样子封装好不好,毕竟没有大项目的开发经验,也不一定直接用这个来操作吧,只是方便自己用来查阅,免得每次都需要百度Google。

    - 获取应用沙盒根目录
    +(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;
    }
    

    相关文章

      网友评论

        本文标题:iOS文件操作

        本文链接:https://www.haomeiwen.com/subject/nsvbcttx.html