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

本文介绍iOS应用中不同类型的文件存储方式,包括Documents、Caches及tmp文件夹的使用场景与注意事项,并提供了实现这些功能的具体代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

#import <Foundation/Foundation.h>

@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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值