文件管理 - 1

来源:互联网 发布:vcr制作软件破解 编辑:程序博客网 时间:2024/06/03 18:29

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.文件写入,如果不存在文件,会自动创建文件


0 0
原创粉丝点击