1. File storage
iOS sandbox mechanism: applications can only access directories created by the application in the file system,
Documents folder: Document folder, storing persisted data,
Library folder: Caches: Store cache files, restart or exit the program, the data is not lost
Perferences: Preferences, store user settings information
TMP folder: Store temporary cache data, empty when restarting or exiting the program;
Basic file operations:
-(void) Viewdidload
{
[Super Viewdidload];
File Manager
Nsfilemanager
Create a file Management object
Nsfilemanager *FM = [Nsfilemanager defaultmanager];
Get Documents folder path
Nsarray *array = Nssearchpathfordirectoriesindomains(nsdocumentdirectory, Nsuserdomainmask, YES );
nsstring *documentpath = [array objectatindex:0];
Create a folder
1. create a path to a folder
NSString *directorypath = [documentpath stringbyappendingpathcomponent:@ "testfile"];//set folder path, The stringByAppendingPathComponent method automatically uses "/" to separate the full path,
[FM createdirectoryatpath:d irectorypath withintermediatedirectories:YES attributes:Nil error:nil];
2. Create a file
The path of the stitching file
NSString *filepath = [DirectoryPath stringbyappendingpathcomponent:@ "file"];
NSData used to package data.
Binary data is stored.
Can save various data strings audio image Array dictionary
Convert a string to data
nsstring *string = @ " Hello ";
UTF8 Encoding
NSData *data = [string datausingencoding: nsutf8stringencoding];
Create a file
[FM createfileatpath: FilePath contents:d ata attributes:nil];
Determine if a file exists
BOOL success = [FM fileexistsatpath:filepath];
NSLog (@ " file%d exists ", success);
To get the path of all the files under a path
Nsarray *subpatharray = [FM subpathsatpath:directorypath];
For (nsstring *path in subpatharray)
{
NSLog (@ "---->%@", path);
}
nsstring *path2 = [DirectoryPath stringbyappendingpathcomponent:@ "file copy "];
Determine if content is equal
Contentsequalatpath
BOOL SUCCESS1 = [fm contentsequalatpath:filepath andpath:path2];
NSLog(@ "Success1 = =%d", success1);
//Move an element under a path to another path
NSString *destinationpath = [nshomedirectory() stringbyappendingpathcomponent:@ "A.txt"];
Move a file to a file path (not a directory)
[FM moveitematpath:filepath topath:destinationpath error:nil];
Copy to a file path
[FM copyitematpath:destinationpath topath:filepath error:nil];
Remove a file from a path
[FM removeitematpath:filepath error:nil];
}
iOS data storage