文件管理 - 1

iOS的文件管理,参考:http://blog.csdn.net/xyz_lmn/article/details/8968213


照着它封装了一下:

#import <Foundation/Foundation.h>

@interface LBFileManager : NSObject


/**
 *  @brief 隐藏视图
 *
 *  @return 全局单例
 */
+ (LBFileManager *)defaultManager;


/**
 *  @brief 获取应用沙盒根路径
 *
 */
- (void)dirHome;


/**
 *  @brief 获取Documents目录路径
 *
 */
- (NSString *)dirDoc;


/**
 *  @brief 获取Library目录路径
 *
 */
- (void)dirLib;


/**
 *  @brief 获取Cache目录
 *
 */
- (void)dirCache;


/**
 *  @brief 获取Tmp目录路径
 *
 */
- (void)dirTmp;


/**
 *  @brief 创建文件夹
 *
 */
- (void)createDirectory:(NSString *)directory;


/**
 *  @brief 创建文件
 *
 */
- (void)createFile:(NSString *)fileName fileType:(NSString *)type;


/**
 *  @brief 将内容写入文件内
 *
 */
- (void)writeFile:(NSString *)fileName fileType:(NSString *)type content:(NSString *)content;


/**
 *  @brief 读文件数据
 *
 */
- (void)readFile:(NSString *)fileName fileType:(NSString *)type;


/**
 *  @brief 读取文件属性,返回一个数组,内含多个属性信息
 *
 */
- (void)fileAttriutesFile:(NSString *)fileName fileType:(NSString *)type;



/**
 *  @brief 删除一个文件
 *
 */
- (void)deleteFile:(NSString *)fileName fileType:(NSString *)type;


@end



实现:

#import "LBFileManager.h"

@interface LBFileManager ()

@end

@implementation LBFileManager

+ (LBFileManager *)defaultManager{

    static LBFileManager *fileManager = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
    
        fileManager = [[[self class] alloc] init];
    
    });
    
    return fileManager;
}

- (void)dirHome{
    NSString *dirHome = NSHomeDirectory();
    NSLog(@"APP_Home: %@", dirHome);
}


- (NSString *)dirDoc{
    //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSLog(@"DocumentDirectory's path : %@", paths);
    
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"APP_Home_Document: %@", documentsDirectory);
    return documentsDirectory;
}


- (void)dirLib{
    //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    
    NSLog(@"NSLibraryDirectory's path : %@", paths);
    
    NSString *libraryDirectory = [paths objectAtIndex:0];
    NSLog(@"APP_Home_Library: %@",libraryDirectory);
}


- (void)dirCache{
    NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    
    NSLog(@"NSCachesDirectory's path : %@", cacPath);
    
    NSString *cachePath = [cacPath objectAtIndex:0];
    NSLog(@"APP_Home_Library_Cache: %@",cachePath);
}


- (void)dirTmp{
    //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
    NSString *tmpDirectory = NSTemporaryDirectory();
    NSLog(@"APP_Home_Tmp: %@",tmpDirectory);
}


#pragma mark -  - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


- (void)createDirectory:(NSString *)directory{
    
    NSString *documentsPath = [self dirDoc];
    NSLog(@"createDirectory : %@", documentsPath);
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *createDirectory = [documentsPath stringByAppendingPathComponent:directory];
    
    // 创建目录
    BOOL res = [fileManager createDirectoryAtPath:createDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    
    if (res) {
        NSLog(@"文件夹创建成功");
    }else
        NSLog(@"文件夹创建失败");
    
}


- (void)createFile:(NSString *)fileName fileType:(NSString *)type{
    
    NSString *documentsPath =[self dirDoc];
    NSLog(@"createDirectory : %@", documentsPath);

    
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:fileName];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *testPath = [testDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", fileName, type]];
    
    BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];
    
    if (res) {
        NSLog(@"文件创建成功: %@" ,testPath);
    }else
        NSLog(@"文件创建失败");
}


- (void)writeFile:(NSString *)fileName fileType:(NSString *)type content:(NSString *)content{
    
    NSString *documentsPath =[self dirDoc];
    NSLog(@"createDirectory : %@", documentsPath);

    
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:fileName];
    
    NSString *testPath = [testDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", fileName, type]];
    
    BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    
    // 覆盖问题:http://www.cocoachina.com/bbs/read.php?tid=92481
    // 解决方法
    // 1.先读出来,再一起写进入.
    // 2.改用数据库.
    // 3.NSFileHandle
    
    if (res) {
        NSLog(@"文件写入成功");
    }else
        NSLog(@"文件写入失败");
}


- (void)readFile:(NSString *)fileName fileType:(NSString *)type{
    
    NSString *documentsPath =[self dirDoc];
    NSLog(@"createDirectory : %@", documentsPath);

    
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:fileName];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", fileName, type]];
    
    //    NSData *data = [NSData dataWithContentsOfFile:testPath];
    //    NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
    
    NSLog(@"文件读取成功: %@",content);
}

 
- (void)fileAttriutesFile:(NSString *)fileName fileType:(NSString *)type{
    
    NSString *documentsPath =[self dirDoc];
    NSLog(@"createDirectory : %@", documentsPath);

    
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:fileName];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", fileName, type]];
    
    NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];
    
    NSArray *keys;
    id key, value;
    keys = [fileAttributes allKeys];
    NSInteger count = [keys count];
    
    for (int i = 0; i < count; i++){
        
        key = [keys objectAtIndex: i];
        value = [fileAttributes objectForKey: key];
        NSLog (@"Key: %@ for value: %@", key, value);
    }
}


- (void)deleteFile:(NSString *)fileName fileType:(NSString *)type{
    
    NSString *documentsPath =[self dirDoc];
    NSLog(@"createDirectory : %@", documentsPath);

    
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:fileName];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *testPath = [testDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", fileName, type]];
    
    BOOL res=[fileManager removeItemAtPath:testPath error:nil];
    
    if (res) {
        NSLog(@"文件删除成功");
    }else
        NSLog(@"文件删除失败");
    NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");
    
}


@end


有几个需要注意的:

1.函数返回为BOOL类型,在逻辑判断中非常好用:

if ([self createFolder:[self downloadPath]]) {
}



2.文件写入,如果不存在文件,会自动创建文件


很不错的文件管理器.Total Commander功能强大的全能文件管理器(简称 TC)。支持随意自定义的菜单、工具栏、快捷键,给您最大的自由,打造个性 TC。一般的文件操作,如搜索、复制、移动、改名、删除等功能应有尽有,更有文件内容比较、同步文件夹、批量重命名文件、分割合并文件、创建/检查文件校验 (MD5/SFV) 等实用功能。内置 ZIP/TAR/GZ/TGZ 格式的压缩/解压功能,ZIP 格式还支持创建加密及自解包功能。此外,不仅可以直接打开(解开) ARJ/CAB/RAR/LZH/ACE/UC2 等压缩包,配合插件或相应的压缩程序,更可创建这些格式的压缩包,就像创建和打开文件夹一样简单。而搜索功能,无论是文件还是内容,同样支持在这些压缩包中进行。 除了支持详细资料、列表、缩略图等资源管理器也有的显示方式外,利用内容插件,支持各式各样的自定义列视图,显示不同文件的内部信息,满足您查看不同文件的需求。多标签界面,让本来的两个窗口,可以扩展到几十个,操作间更显自由。而贴心的历史和常用文件夹按钮,可以导出/导入的标签/文件列表,让您穿越各个间更加快捷高效。 通过工具栏和菜单,可以随意调用外部程序和内部命令。多种功能或强大或实用的插件,让 TC 如虎添翼、能原来所不能。批量重命名和文件搜索,利用插件和正则表达式,功能更多更强了。还有可以选择的美化界面,支持 FXP 和 HTTP 代理的 FTP 功能,等等等等……有了它,资源管理器、WinZip 及大多数文件管理和工具栏软件,都可以下岗了!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值