iOS 沙盒文件操作封装

2020-09-05  本文已影响0人  邓布利多教授

对沙盒路径下文件操作的简单封装,先上源码,再介绍使用方式,真的超级简单。

源码

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface LYFileTools : NSObject

/**
 文件写入
 */
+(BOOL)fileWriteWithFilename:(NSString *)fileName data:(NSData *)data;

/**
 文件删除
 */
+(BOOL)fileDeleteWithFilename:(NSString *)fileName;
+(void)fileAllDeleteAtPath;

/**
 文件查找
 */
+(BOOL)fileFindAtPath:(NSString*)fileName;

/**
 文件读取
 */
+(NSString *)fileReadWithFilename:(NSString *)fileName;

@end

NS_ASSUME_NONNULL_END

#import "LYFileTools.h"

@implementation LYFileTools

#pragma mark - 文件写入
+(BOOL)fileWriteWithFilename:(NSString *)fileName data:(NSData *)data{
    
    NSString *path = [self fileExisted];
    
    //判断文件路径是否存在,存在时查找对应的文件,否则返回NO
    if ([path isEqualToString:@""]) {
        return NO;
    }else{
        
        //文件路径存在,写入数据
        NSString *filePath = [path stringByAppendingPathComponent:fileName];
        [data writeToFile:filePath atomically:YES];
        return YES;
        
    }
    
}

#pragma mark - 文件删除
+(BOOL)fileDeleteWithFilename:(NSString *)fileName{
    
    NSString *path = [self fileExisted];
    
    //判断文件路径是否存在,存在时查找对应的文件,否则返回NO
    if ([path isEqualToString:@""]) {
        return NO;
    }else{
        NSFileManager *fileManager = [NSFileManager defaultManager];
        return [fileManager removeItemAtPath:[path stringByAppendingFormat:@"/%@",fileName] error:nil];
    }
    
}

+(void)fileAllDeleteAtPath{
    
    NSString *path = [self fileExisted];
    
    //判断文件路径是否存在,存在时查找对应的文件,否则return
    if ([path isEqualToString:@""]) {
        return;
    }else{
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager removeItemAtPath:path error:nil];
    }
    
}

#pragma mark - 文件查找
+(BOOL)fileFindAtPath:(NSString*)fileName{
    
    NSString *path = [self fileExisted];
    
    if ([path isEqualToString:@""]) {
        return NO;
    }else{

        NSFileManager *fileManager = [NSFileManager defaultManager];
        //全部文件名
        NSArray *tempArray = [fileManager contentsOfDirectoryAtPath:path error:nil];
        
        for (NSString *name in tempArray) {
            if ([fileName isEqualToString:name]) {
                return YES;
            }
        }
        return NO;
        
    }
    
}

#pragma mark - 文件读取
+(NSString *)fileReadWithFilename:(NSString *)fileName{
    
    NSString *path = [self fileExisted];
    
    if ([path isEqualToString:@""]) {
        return @"";
    }else{
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        //全部文件名
        NSArray *tempArray = [fileManager contentsOfDirectoryAtPath:path error:nil];
        
        for (NSString *name in tempArray) {
            if ([fileName isEqualToString:name]) {
                return [path stringByAppendingFormat:@"/%@",fileName];
            }
        }
        return @"";
    }
    
}


#pragma mark - 文件路径是否存在
+(NSString *)fileExisted{
    
    NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    NSString *fileCaches = [NSString stringWithFormat:@"%@/FileCaches", path];
    
    BOOL isDir = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //是否存在该文件目录
    BOOL existed = [fileManager fileExistsAtPath:fileCaches isDirectory:&isDir];
    
    BOOL create = NO;//文件创建是否成功
    //文件目录不存在,创建文件
    if ( !isDir && !existed) {
        create = [fileManager createDirectoryAtPath:fileCaches withIntermediateDirectories:YES attributes:nil error:nil];
    }else{
        create = YES;
    }

    //文件路径存在时,返回正确路径,否则返回空字符串
    if (create) {
        return fileCaches;
    }else{
        return @"";
    }
    
}

@end

使用介绍

NSData *data = [@"abcdefg" dataUsingEncoding:NSUTF8StringEncoding];
[LYFileTools fileWriteWithFilename:@"string" data:data];
NSString *path = [LYFileTools fileReadWithFilename:@"string"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSString *content = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"content = %@",content);
[LYFileTools fileFindAtPath:@"string"];
[LYFileTools fileDeleteWithFilename:@"string"];
[LYFileTools fileAllDeleteAtPath];

全剧终

上一篇 下一篇

猜你喜欢

热点阅读