iOS 利用FZEasyFile本地保存 和 常规保存

1、常规保存(较麻烦)

NSFileManager *fileManager = [NSFileManager defaultManager];

    //获取document路径,括号中属性为当前应用程序独享
    NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [directoryPaths objectAtIndex:0];

    //查找文件夹,如果不存在,就创建一个文件夹
    NSString *dir = [documentDirectory stringByAppendingPathComponent:@SAVEDIR];
    NSLog(@"cache dir %@", dir);
    if(![fileManager fileExistsAtPath:dir])
    {
        if(![fileManager createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:nil])
        {
            NSLog(@"create dir:(%@) error", dir);
            return;
        }
    }

    //定义记录文件全名以及路径的字符串filePath
    NSString *filePath = [dir stringByAppendingPathComponent:[[NSString alloc]initWithFormat:@"/%@", filename]];

    //查找文件,如果不存在,就创建一个文件
    NSData *data = [lHtml dataUsingEncoding:NSUTF8StringEncoding];
    if (![fileManager fileExistsAtPath:filePath]) {
        [fileManager createFileAtPath:filePath contents:data attributes:nil];
    }


2、FZEasyFile保存
导入的文件:

//

//  FZEasyFile.h

//  FZEasyFile

//

//  Created by zhou jun on 14-4-29.

//  Copyright (c) 2014年 shannonchou. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface FZEasyFile : NSObject

/**

 singleton pattern.

 @return the shared instance.

 */

+ (FZEasyFile *)sharedInstance;

/**

 convert the short file name to full file name. e.g. "mycache/user/icon.png" -> "/Users/zhoujun/Library/Application Support/iPhone Simulator/7.1/Applications/ABCE2119-E864-4492-A3A9-A238ADA74BE5/Documents/mycache/user/icon.png".

 @return full file name.

 */

- (NSString *)fullFileName:(NSString *)shortFileName;

/**

 test if the file exists.

 @param fileName file path and file name, e.g. "mycache/user/icon.png".

 @return YES if exists, NO otherwise.

 */

- (BOOL)isFileExists:(NSString *)fileName;

/**

 create a file

 @param fileName fileName file path and file name, e.g. "mycache/user/icon.png".

 @param shouldOverwrite YES:if the file exists then overwirte it, NO:if the file exists then do nothing

 */

- (void)createFile:(NSString *)fileName overwrite:(BOOL)shouldOverwrite;

@end

//

//  FZEasyFile.m

//  FZEasyFile

//

//  Created by zhou jun on 14-4-29.

//  Copyright (c) 2014年 shannonchou. All rights reserved.

//

#import "FZEasyFile.h"

@implementation FZEasyFile

static FZEasyFile *instance;

+ (FZEasyFile *)sharedInstance {

    if (!instance) {

        @synchronized (FZEasyFile.class){

            if (!instance) {

                instance = [[FZEasyFile alloc] init];

            }

        }

    }

    return instance;

}

- (NSString *)fullFileName:(NSString *)shortFileName {

    //search the "document" path

    NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentDirectory = [directoryPaths objectAtIndex:0];

    

    NSString *file = [documentDirectory stringByAppendingPathComponent:shortFileName];

    return file;

}

- (BOOL)isFileExists:(NSString *)fileName {

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *file = [self fullFileName:fileName];

    return [fileManager fileExistsAtPath:file];

}

- (void)createFile:(NSString *)fileName overwrite:(BOOL)shouldOverwrite {

    NSFileManager *fileManager = [NSFileManager defaultManager];

    

    //create file directory, include multilayer directory

    NSRange lastTag = [fileName rangeOfString:@"/" options:NSBackwardsSearch];

    if (lastTag.location != NSNotFound && lastTag.location != 0) {

        NSString *shortDir = [fileName substringToIndex:lastTag.location];

        NSString *fullDir = [self fullFileName:shortDir];

        NSLog(@"full directory: %@", fullDir);

        if (![fileManager fileExistsAtPath:fullDir]) {

            [fileManager createDirectoryAtPath:fullDir withIntermediateDirectories:YES attributes:nil error:nil];

        }

    }

    

    NSString *file = [self fullFileName:fileName];

    NSLog(@"full file name:%@", file);

    

    //file not exists or want to overwrite it

    if (shouldOverwrite || ![fileManager fileExistsAtPath:file]) {

        BOOL suc = [fileManager createFileAtPath:file contents:nil attributes:nil];

        NSLog(@"create file(%@) %@", file, suc ? @"successfully" : @"failed");

    }

}

@end

使用方法:

导入头文件:


#import "FZEasyFile.h"

判断地址是否已存在


[EasyFile isFileExists:@"my/file/path/info.txt"]

创建


[EasyFile createFile:"my/file/path/info.txt" overwrite:NO];

写入文件中:拼接和覆盖写入


[FZEasyFile writeFile:"my/file/path/info.txt" contents:[@"a" dataUsingEncoding:NSUTF8StringEncoding] append:NO];
[FZEasyFile writeFile:"my/file/path/info.txt" contents:[@"b" dataUsingEncoding:NSUTF8StringEncoding] append:YES];

After these calling the content of the file is "ab".


删除文件


[FZEasyFile removeFile:"my/file/path/info.txt"];
[FZEasyFile removeFile:"my/file/path"];

获取绝对路径(实际存储路径)


NSString *fullName = [EasyFile fullFileName:"my/file/path/info.txt"];

转换

After getting the full name, you can pass it to other API, such as NSInputStream:


NSInputStream *input = [NSInputStream inputStreamWithFileAtPath:fullName];

 

 
原文地址:https://www.cnblogs.com/niexiaobo/p/4646184.html