iOS文件处理类

简介:

iOS文件处理类

这是一个用来简化iOS中关于文件操作的一个类,所有方法都为类方法.

 

Source

File.h

//
//  File.h
//  FileManager
//
//  http://home.cnblogs.com/u/YouXianMing/
//
//  Copyright (c) 2014年 YouXianMing All rights reserved.
//

#import <Foundation/Foundation.h>

#define FILE_PATH(filePath)       [File path:(filePath)]
#define ROOT_PATH                 [File rootPath]
#define BUNDLE_PATH(fileName)     [File bundleSource:(fileName)]
#define CREATE_FOLDER(folderPath) [File createFolder:(folderPath)]
#define FILE_EXIST(filePath)      [File exist:(filePath)]
#define IS_DIRECTORY(filePath)    [File isDirectory:(filePath)]
#define IS_FILE(filePath)         ![File isDirectory:(filePath)]
#define FILE_INFO(filePath)       [File fileInfo:(filePath)]
#define FILE_SIZE(filePath)       [File fileSize:(filePath)]
#define FILE_DICTIONARY(filePath) [File dictionaryFrom:(filePath)]
#define FILE_ARRAY(filePath)      [File arrayFrom:(filePath)]

@interface File : NSObject

/*
 注意:凡是参数为 filePath folderPath 的全部为相对路径,用以下字符串拼接使用
 
 /Documents
 /Library/Caches
 /Library/Preferences
 /tmp
 
 其他都为绝对路径
 */

+ (NSString *)rootPath;
+ (NSString *)bundleSource:(NSString *)fileName;
+ (NSString *)path:(NSString *)filePath;
+ (BOOL)createFolder:(NSString *)filePath;
+ (BOOL)exist:(NSString *)filePath;
+ (BOOL)isDirectory:(NSString *)filePath;
+ (NSDictionary *)fileInfo:(NSString *)filePath;
+ (int)fileSize:(NSString *)filePath;
+ (NSArray *)enumeratorFolder:(NSString *)folderPath;
+ (void)enumeratorFolder:(NSString *)folderPath each:(void (^)(NSString *path))each;

+ (BOOL)copyFrom:(NSString *)sourcePath to:(NSString *)targetPath;
+ (BOOL)moveFrom:(NSString *)sourcePath to:(NSString *)targetPath;
+ (BOOL)remove:(NSString *)targetPath;

+ (BOOL)writePlist:(id)plist to:(NSString *)filePath;
+ (NSMutableDictionary *)dictionaryFrom:(NSString *)filePath;
+ (NSMutableArray *)arrayFrom:(NSString *)filePath;
+ (void)path:(NSString *)filePath dictionary:(void (^)(NSMutableDictionary *dictionary))dictionary;
+ (void)path:(NSString *)filePath array:(void (^)(NSMutableArray *array))array;

@end

File.m


//
//  File.m
//  FileManager
//
//  http://home.cnblogs.com/u/YouXianMing/
//
//  Copyright (c) 2014年 YouXianMing All rights reserved.
//

#import "File.h"

static NSString *_sandBoxPath = nil;

@implementation File

+ (void)initialize
{
    if (self == [File class])
    {
        _sandBoxPath = NSHomeDirectory();
    }
}

+ (NSString *)rootPath
{
    return _sandBoxPath;
}

+ (NSString *)path:(NSString *)filePath
{
    return [_sandBoxPath stringByAppendingPathComponent:filePath];
}

+ (BOOL)createFolder:(NSString *)filePath
{
    return [[NSFileManager defaultManager] createDirectoryAtPath:[self path:filePath]
                                     withIntermediateDirectories:YES
                                                      attributes:nil
                                                           error:nil];
}

+ (NSString *)bundleSource:(NSString *)fileName
{
    return [[NSBundle mainBundle] pathForResource:fileName
                                           ofType:nil];
}

+ (BOOL)exist:(NSString *)filePath
{
    return [[NSFileManager defaultManager] fileExistsAtPath:[self path:filePath]
                                                isDirectory:NO];
}

+ (BOOL)isDirectory:(NSString *)filePath
{
    BOOL isDirectory = NO;
    
    [[NSFileManager defaultManager] fileExistsAtPath:[self path:filePath]
                                         isDirectory:&isDirectory];
    
    return isDirectory;
}

+ (BOOL)copyFrom:(NSString *)sourcePath to:(NSString *)targetPath
{
    return [[NSFileManager defaultManager] copyItemAtPath:sourcePath
                                                   toPath:targetPath
                                                    error:nil];
}

+ (BOOL)moveFrom:(NSString *)sourcePath to:(NSString *)targetPath
{
    return [[NSFileManager defaultManager] moveItemAtPath:sourcePath
                                                   toPath:targetPath
                                                    error:nil];
}

+ (BOOL)remove:(NSString *)targetPath
{
    return [[NSFileManager defaultManager] removeItemAtPath:targetPath
                                                      error:nil];
}

+ (NSArray *)enumeratorFolder:(NSString *)folderPath
{
    if ([self isDirectory:folderPath])
    {
        NSMutableArray *storeArray = [NSMutableArray array];
        
        NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:folderPath];
        NSFileManager *localFileManager = [[NSFileManager alloc] init];
        NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:docsDir];
        
        NSString *file;
        while ((file = [dirEnum nextObject]))
        {
            [storeArray addObject:[folderPath stringByAppendingPathComponent:file]];
        }
        
        return storeArray;
    }
    else
    {
        return nil;
    }
}

+ (void)enumeratorFolder:(NSString *)folderPath each:(void (^)(NSString *path))each
{
    if ([self isDirectory:folderPath])
    {
        NSMutableArray *storeArray = [NSMutableArray array];
        
        NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:folderPath];
        NSFileManager *localFileManager = [[NSFileManager alloc] init];
        NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:docsDir];
        
        NSString *file;
        while ((file = [dirEnum nextObject]))
        {
            [storeArray addObject:[folderPath stringByAppendingPathComponent:file]];
        }
        
        [storeArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            each(obj);
        }];
    }
}

+ (NSDictionary *)fileInfo:(NSString *)filePath
{
    return [[NSFileManager defaultManager] attributesOfItemAtPath:[self path:filePath]
                                                            error:nil];
}

+ (int)fileSize:(NSString *)filePath
{
    return [[[[NSFileManager defaultManager] attributesOfItemAtPath:[self path:filePath]
                                                              error:nil] \
             objectForKey:@"NSFileSize"] intValue];
}

+ (BOOL)writePlist:(id)plist to:(NSString *)filePath
{
    if ([plist isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *point = plist;
        
        return [point writeToFile:[self path:filePath]
                       atomically:YES];
    }
    else if ([plist isKindOfClass:[NSArray class]])
    {
        NSArray *point = plist;
        
        return [point writeToFile:[self path:filePath]
                       atomically:YES];
    }
    else
    {
        return NO;
    }
}

+ (NSMutableDictionary *)dictionaryFrom:(NSString *)filePath
{
    NSMutableDictionary *dictionary = \
        [[NSMutableDictionary alloc] initWithContentsOfFile:[self path:filePath]];
    
    return dictionary;
}

+ (NSMutableArray *)arrayFrom:(NSString *)filePath
{
    NSMutableArray *array = \
        [[NSMutableArray alloc] initWithContentsOfFile:[self path:filePath]];
    
    return array;
}

+ (void)path:(NSString *)filePath dictionary:(void (^)(NSMutableDictionary *dictionary))dictionary
{
    NSMutableDictionary *sourceDictionary = \
        [[NSMutableDictionary alloc] initWithContentsOfFile:[self path:filePath]];
    
    dictionary(sourceDictionary);
    
    [sourceDictionary writeToFile:[self path:filePath]
                       atomically:YES];
}

+ (void)path:(NSString *)filePath array:(void (^)(NSMutableArray *array))array
{
    NSMutableArray *sourceArray = \
    [[NSMutableArray alloc] initWithContentsOfFile:[self path:filePath]];
    
    array(sourceArray);
    
    [sourceArray writeToFile:[self path:filePath]
                  atomically:YES];
}

@end

Usage

获取沙盒中Documents目录下的一个文本文件YouXianMing.txt

判断这个沙盒中Documents目录下的一个文本文件YouXianMing.txt是否存在

判断沙盒中Documents目录下的一个文本文件YouXianMing.txt是否为一个文件

沙盒中Documents目录下的一个文本文件YouXianMing.txt的大小

读取/Library/Preferences目录下的一个字典类型的plist文件YouXianMing.plist

在沙盒根目录下创建文件夹层级结构/A/B/C

将根目录下的A文件夹以及子文件夹拷贝到Documents下的A文件夹当中

将集合(数组或者字典)写到/Library/Preferences下的tmp.plist当中

读取/Library/Preferences目录下的tmp.plist,修改其中的一个值并保存

没举例子的自己试一下就知道了.

so easy :) enjoy it.



目录
相关文章
|
测试技术 程序员 C++
iOS:项目中无用类检测和无用图片检测汇总
在涉及到项目大改版,或者涉及到某个功能模块大变更,就会涉及到图片废弃和文件废弃的情况。 但是这时候就会遗留下一个很大的问题,没有将废弃的、无用的文件类或资源删除干净。而这次需要对工程代码的无用资源和无用文件进行删除处理,感触颇多,故在此笔记。 首先,感觉很多人的代码习惯还是恶待提高。比如我发现一些人的代码操作习惯,从好到次,可以大略分以下情况
1286 0
iOS:项目中无用类检测和无用图片检测汇总
|
2月前
|
Swift iOS开发
6-7|IOS如何定义一个类
6-7|IOS如何定义一个类
|
3月前
|
存储 iOS开发
iOS 16 系统键盘修复问题之确定UIKeyboardTaskQueue类对_lock的加锁和解锁操作如何解决
iOS 16 系统键盘修复问题之确定UIKeyboardTaskQueue类对_lock的加锁和解锁操作如何解决
|
3月前
|
存储 安全 iOS开发
iOS 16 系统键盘修复问题之确定UIKeyboardTaskQueue类中对_lock的使用是否正确如何解决
iOS 16 系统键盘修复问题之确定UIKeyboardTaskQueue类中对_lock的使用是否正确如何解决
|
设计模式 编译器 C++
iOS-底层原理 19:类扩展 与 关联对象 底层原理探索
iOS-底层原理 19:类扩展 与 关联对象 底层原理探索
140 0
iOS-底层原理 19:类扩展 与 关联对象 底层原理探索
|
存储 iOS开发
iOS-底层原理 18:类的加载(下)
iOS-底层原理 18:类的加载(下)
133 0
iOS-底层原理 18:类的加载(下)
|
存储 缓存 算法
iOS-底层原理 17:类的加载(上)
iOS-底层原理 17:类的加载(上)
198 0
iOS-底层原理 17:类的加载(上)
|
存储 C++
iOS-底层原理 09:类 & isa 经典面试题分析
iOS-底层原理 09:类 & isa 经典面试题分析
163 0
iOS-底层原理 09:类 & isa 经典面试题分析
|
存储 编译器 C++
iOS-底层原理 08:类 & 类结构分析
iOS-底层原理 08:类 & 类结构分析
158 0
iOS-底层原理 08:类 & 类结构分析
|
存储 设计模式 编译器
iOS-底层原理 07:isa与类关联的原理
iOS-底层原理 07:isa与类关联的原理
120 0
iOS-底层原理 07:isa与类关联的原理