1. Regular saving (more trouble)
Nsfilemanager *filemanager = [NsfilemanagerDefaultmanager];Gets the document path, with the parentheses property being exclusive to the current applicationNsarray *directorypaths =Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask,YES);NSString *documentdirectory = [directorypathsObjectatindex:0];Find folder, create a folder if it doesn't existNSString *dir = [documentdirectorystringByAppendingPathComponent: @SAVEDIR];NSLog (@ "Cache dir%@", dir);if (![ FileManagerFileexistsatpath:dir]) {if (![ FileManagerCreatedirectoryatpath:dirWithintermediatedirectories:YESAttributesNilErrorNil]) {NSLog (@ "Create dir: (%@) Error", dir); return;} } //define the full name of the record file and the path of the string FilePath nsstring *filepath = [dir stringbyappendingpathcomponent:[[nsstring Alloc]initwithformat:@ "/%@", filename]]; //Find file, if not present, create a file nsdata *data = [lhtml datausingencoding:nsutf8stringencoding]; if (![ FileManager Fileexistsatpath:filepath]) {[FileManager createfileatpath:filepath contents:data Attributes:nil]; }
2, Fzeasyfile Save
Imported Files:
//
FZEasyFile.h
Fzeasyfile
//
Created by Zhou June 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 and no:if the file exists then does nothing
*/
-(void) CreateFile: (NSString *) fileName Overwrite: (BOOL) Shouldoverwrite;
@end
//
FZEASYFILE.M
Fzeasyfile
//
Created by Zhou June 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
How to use:
Import Header file:
#"FZEasyFile.h"
Determine if the address already exists
Isfileexists:@ "My/file/path/info.txt"]
Create
CreateFile:"My/file/path/info.txtOverwrite:NO];
Write to file: Splice and Overwrite write
WriteFile:"My/file/path/info.txtcontents:[@" Aappend:writefile:"my/file/path/ Info.txtcontents:[@ "bappend:YES];
After these calling, the content of the file is "AB".
deleting files
RemoveFile: "my/file/path/info.txtremovefile:" My/file/path"];
Get absolute path (actual storage path)
Fullfilename:"My/file/path/info.txt"];
Transformation
After getting the full name, you can pass it to the other API, such as Nsinputstream:
Nsinputstream *input = [inputstreamwithfileatpath:fullname];
IOS leverages Fzeasyfile local save and regular save