CocoaPods trunk is moving to be read-only. Read more on the blog, there are 9 months to go.

FZEasyFile 0.0.3

FZEasyFile 0.0.3

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Shannon Chou.



  • By
  • Shannon

An easy way to create, overwrite and remove file in sandbox.

What is it?

It’s tedious, boring and unreadable to use iOS’s sdk to manage file directly. FZEasyFile provides an easy way to create or overwrite file in sandbox. If you want to create a file, you just tell FZEasyFile your file name, which is "my/file/path/info.txt". You don’t need to care about what the root directory is or whether the directory "my/file/path/" exists.

Actually, the root directory is "Documents" in sandbox, "my/file/path" will be create automatically if not exists.

Usage

Import the source files

Drag "FZEasyFile.h" and "FZEasyFile.m" to your project. And import .h file in your source file:

#import "FZEasyFile.h"

Test whether the file exists

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

Create a new file

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

Translate short file name to full one

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];

Comparison

Here I paste two paragraphs of code. Respectively, one using traditional way, the other using FZEasyFile. Creat a new file in traditional way:

    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];
    }

creat a new file using FZEasyFile:

[EasyFile createFile:fileName overwrite:NO];
NSOutputStream *output = [NSOutputStream outputStreamToFileAtPath:[EasyFile fullFileName:fileName] append:NO];

Contact

Email me : [email protected]

License

MIT License. See the LICENSE file.