This article focuses on file operations in the sandbox storage mode. The details are as follows:
For an app running on the iPhone, it can only access some files under its root directory (so-called sandbox-sandbox ).
After an app is published to the iPhone, its directory structure is as follows:
1. The app root can be accessed using NSHomeDirectory;
2. The Documents directory is the place where we can write and save the file. We can generally get it in the following way:
[Cpp] view plaincopyprint? NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * documentsDirectory = [paths objectAtIndex: 0];
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * documentsDirectory = [paths objectAtIndex: 0];
3. In the tmp directory, we can write data that is needed when the program is running. The data written in the tmp directory will not exist after the program exits. You can use
NSString * NSTemporaryDirectory (void); method;
4. file operations can be performed through NSFileManage, And the instance can be obtained through [NSFileManger defaultManger.
Related Operations:
A. Create a directory or file:
For example, if you want to create a test directory under Documents,
[Cpp]
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * documentsDirectory = [paths objectAtIndex: 0];
NSLog (@ "% @", documentsDirectory );
NSFileManager * fileManage = [NSFileManager defaultManager];
NSString * myDirectory = [documentsDirectory stringByAppendingPathComponent: @ "test"];
BOOL OK = [fileManage createDirectoryAtPath: myDirectory attributes: nil];
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * documentsDirectory = [paths objectAtIndex: 0];
NSLog (@ "% @", documentsDirectory );
NSFileManager * fileManage = [NSFileManager defaultManager];
NSString * myDirectory = [documentsDirectory stringByAppendingPathComponent: @ "test"];
BOOL OK = [fileManage createDirectoryAtPath: myDirectory attributes: nil];
To create a file.txt file under documents:
[Html]
// The result is/Documents/file.txt.
NSString * filePath = [documentsDirectory stringByAppendingPathComponent: @ "file.txt"];
// The result is/Documents/file.txt.
NSString * filePath = [documentsDirectory stringByAppendingPathComponent: @ "file.txt"];
B. Get all file names in a directory:
[Cpp]
// The above myDirectory is available
NSArray * file = [fileManager subpathsOfDirectoryAtPath: myDirectory error: nil];
Or
NSArray * files = [fileManager subpathsAtPath: myDirectory];
// The above myDirectory is available
NSArray * file = [fileManager subpathsOfDirectoryAtPath: myDirectory error: nil];
Or
NSArray * files = [fileManager subpathsAtPath: myDirectory];
C. Read an object:
[Cpp]
NSData * data = [fileManger contentsAtPath: myFilePath]; // myFilePath is the file name that contains the complete path
Or directly use the NSData class method:
NSData * data = [NSData dataWithContentOfPath: myFilePath];
NSData * data = [fileManger contentsAtPath: myFilePath]; // myFilePath is the file name that contains the complete path
Or directly use the NSData class method:
NSData * data = [NSData dataWithContentOfPath: myFilePath]; d. Save a file:
[Cpp] view plaincopyprint? // You can use NSFileManager in the following ways:
-(BOOL) createFileAtPath :( NSString *) path contents :( NSData *) data attributes :( NSDictionary *) attr;
Or NSData
-(BOOL) writeToFile :( NSString *) path atomically :( BOOL) useAuxiliaryFile;
-(BOOL) writeToFile :( NSString *) path options :( NSUInteger) writeOptionsMask error :( NSError **) errorPtr;
// You can use NSFileManager in the following ways:
-(BOOL) createFileAtPath :( NSString *) path contents :( NSData *) data attributes :( NSDictionary *) attr;
Or NSData
-(BOOL) writeToFile :( NSString *) path atomically :( BOOL) useAuxiliaryFile;
-(BOOL) writeToFile :( NSString *) path options :( NSUInteger) writeOptionsMask error :( NSError **) errorPtr;
E. delete an object:
[Cpp] view plaincopyprint? // You can use NSFileManager in the following ways:
// Removes the file or directory at the specified path.
-(BOOL) removeItemAtPath :( NSString *) path error :( NSError **) error
// Removes the file or directory at the specified URL.
-(BOOL) removeItemAtURL :( NSURL *) URL error :( NSError **) error
// You can use NSFileManager in the following ways:
// Removes the file or directory at the specified path.
-(BOOL) removeItemAtPath :( NSString *) path error :( NSError **) error
// Removes the file or directory at the specified URL.
-(BOOL) removeItemAtURL :( NSURL *) URL error :( NSError **) error
F. Move or rename a file
[Cpp]
// To rename an object, we need to move the object to a new path. The following code creates the desired path of the target file, and then requests to move the file and display the file directory after moving it.
// Rename the object by moving the object
NSString * filePath2 = [documentsDirectory stringByAppendingPathComponent: @ "file2.txt"];
// Determine whether to move
If ([fileManager moveItemAtPath: filePath toPath: filePath2 error: & error]! = YES)
NSLog (@ "Unable to move file: % @", [error localizedDescription]);
// Display the contents of the file directory
NSLog (@ "Documentsdirectory: % @", [fileManager contentsOfDirectoryAtPath: documentsDirectoryerror: & error]);
// To rename an object, we need to move the object to a new path. The following code creates the desired path of the target file, and then requests to move the file and display the file directory after moving it.
// Rename the object by moving the object
NSString * filePath2 = [documentsDirectory stringByAppendingPathComponent: @ "file2.txt"];
// Determine whether to move
If ([fileManager moveItemAtPath: filePath toPath: filePath2 error: & error]! = YES)
NSLog (@ "Unable to move file: % @", [error localizedDescription]);
// Display the contents of the file directory
NSLog (@ "Documentsdirectory: % @", [fileManager contentsOfDirectoryAtPath: documentsDirectoryerror: & error]);
How to read and write data using the iPhone official SDK
We know that for security reasons, the official iPhone SDK cannot write files as freely as the toolchain does.
Note: Both methods are stored in/Documents.
[Cpp]
Bool writeApplicationData (NSData * data, NSString * fileName)
{
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * documentsDirectory = [paths objectAtIndex: 0];
If (! DocumentsDirectory ){
NSLog (@ "Documents directory not found! ");
Return NO;
}
NSString * appFile = [documentsDirectory stringByAppendingPathComponent: fileName];
Return ([data writeToFile: appFile atomically: YES]);
}
NSData * applicationDataFromFile (NSString * fileName)
{
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * documentsDirectory = [paths objectAtIndex: 0];
NSString * appFile = [documentsDirectory stringByAppendingPathComponent: fileName];
NSData * myData = [[NSData alloc] initWithContentsOfFile: appFile] autorelease];
Return myData;
}
Bool writeApplicationData (NSData * data, NSString * fileName)
{
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * documentsDirectory = [paths objectAtIndex: 0];
If (! DocumentsDirectory ){
NSLog (@ "Documents directory not found! ");
Return NO;
}
NSString * appFile = [documentsDirectory stringByAppendingPathComponent: fileName];
Return ([data writeToFile: appFile atomically: YES]);
}
NSData * applicationDataFromFile (NSString * fileName)
{
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * documentsDirectory = [paths objectAtIndex: 0];
NSString * appFile = [documentsDirectory stringByAppendingPathComponent: fileName];
NSData * myData = [[NSData alloc] initWithContentsOfFile: appFile] autorelease];
Return myData;
}