IOS工具类--本地沙盒文件基本操作

本类提供在本地文件的读写、判断等操作:

/*
 一:a.Documents文件夹,主要用来存储一些重要的信息,比如用户资料,程序的配置文件,聊天记录等等
     .Documents中存储的信息会随着iTunes同步到电脑备份或者是iCould开发会同步到云端.该文件夹下不可存太大的内容,比如视频等.上传时会被拒掉.
    b.Caches主要存储一些缓存文件,比如视频缓存,音频缓存或者图片缓存等.
    c.tmp主要用于存储一些临时文件,比如做断点续传时的临时文件.
    这三个文件夹存储的内容都需要手动删除,极限情况下,磁盘空间不足的时候操作系统会清除tmp文件夹的内容.
 */

#import 

@interface FileUtils : NSObject


/**
 获取沙盒主目录
 @return 返回目录
 */
+(NSString*) getSandBoxHomeDir;

/**
 获取临时目录
 @return 临时目录
 */
+(NSString*) getTmpDir;


/**
 获取Document目录
 @return 返回目录
 */
+(NSString*) getDocumentDir;


/**
 获取缓存文件目录
 @return 返回缓存文件目录
 */
+(NSString*) getCacheDir;

/**
 判断指定文件是否存在

 @param fileName 文件名
 @return 检测结果
 */
+(BOOL) isExistFile:(NSString*) fileName;

/**
 删除指定文件

 @param fileName 文件名
 @return 返回删除结果
 */
+(BOOL) deleteFile:(NSString*) fileName;

/**
 将字符串写入沙盒

 @param content 待写入的内容
 @param fileName 文件名
 @return 返回写入结果
 */
+(BOOL) writeString2File:(NSString*) content fileName:(NSString*) fileName;

+(NSString*) readFile:(NSString*) fileName;


@end

源文件:

#import "FileUtils.h"

@implementation FileUtils



+(NSString*) getSandBoxHomeDir{

    NSString *homePath = NSHomeDirectory();
    return homePath;
}

+(NSString *)getDocumentDir{

    NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)  firstObject];
    return documentsPath;
}

+(NSString *)getTmpDir{

    NSString * tmpPath = NSTemporaryDirectory();
    return tmpPath;
}

+(NSString*) getCacheDir{
    //NSString *homePath = NSHomeDirectory();
    //方法1
    NSString * cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    //方法2
    //NSString * cachesPath1 = [homePath stringByAppendingString:@"/Library/Caches"];
    //方法3 此api会自动补一个/
    //NSString * cachesPath3 = [homePath stringByAppendingPathComponent:@"Library/Caches"];

    return cachesPath;
}

+(BOOL) isExistFile:(NSString*) fileName{
    NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)  firstObject];
    NSFileManager * fileManager = [NSFileManager defaultManager];
    NSString * filePath = [documentsPath stringByAppendingPathComponent:fileName];
    return [fileManager fileExistsAtPath:filePath];
}

+(BOOL) deleteFile:(NSString*) fileName{

    NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)  firstObject];
    NSFileManager * fileManager = [NSFileManager defaultManager];
    NSString * filePath = [documentsPath stringByAppendingPathComponent:fileName];
    return [fileManager removeItemAtPath:filePath error:nil];
}

+(BOOL) writeString2File:(NSString*) content fileName:(NSString*) fileName{

    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)  firstObject];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName];
    NSLog(@"64------write file path is :%@",filePath);
    NSError *error = nil;
    BOOL result = [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        NSLog(@"error:%@",error);
    }

    return result;
    //数组写入磁盘
//    NSArray * array = @[@"我是",@"好看的",@"数组"];
//    [array writeToFile:[self filePath] atomically:YES];

}

+(NSString*) readFile:(NSString*) fileName{

    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)  firstObject];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName];
    NSLog(@"82------read file path is :%@",filePath);
    //从磁盘读出字符串
    NSString * string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    //读出数组
    //NSArray * array = [NSArray arrayWithContentsOfFile:[self filePath]];

    return string;
}


@end

你可能感兴趣的:(Object-C,IOS)