IOS learning notes-File Operations (NSFileManager) and iosnsfilemanager
IOS sandbox mechanism. Applications can only access files in their app directories. Unlike android, iOS does not have an SD card and cannot directly access images, videos, and other content. Content generated by iOS apps, such as files and cached content, must be stored in your sandbox. By default, each sandbox contains three folders: Documents, Library, and tmp. The Library contains the Caches and Preferences directories.
The complete path above is: User-> resource library-> Application Support-> iPhone Simulator-> 6.1-> Aplications
Documents: Apple recommends that you store the files created by the program and the file data generated by application browsing in this directory. This directory will be included during iTunes backup and recovery.
Library: the default setting of the storage program or other status information;
Library/Caches: stores cached files and stores the persistent data of applications. It is used for data storage after an application upgrade or application is disabled and won't be synchronized by itunes. Therefore, to reduce the synchronization time, you can consider putting some large files that do not need to be backed up into this directory.
Tmp: provides a place to create temporary files in real time, but does not need to be persisted. After the application is closed, the data in this directory will be deleted, or the system may clear the data when the program is not running.
APP Sandbox
How does iOS get the sandbox path and how does it operate files? The answer is given below.
Obtain the application sandbox root path:
- -(Void) dirHome {
- NSString * dirHome = NSHomeDirectory ();
- NSLog (@ "app_home: % @", dirHome );
- }
Obtain the Documents directory path:
- // Obtain the Documents directory
- -(NSString *) dirDoc {
- // [NSHomeDirectory () stringByAppendingPathComponent: @ "events"];
- NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
- NSString * documentsDirectory = [paths objectAtIndex: 0];
- NSLog (@ "app_home_doc: % @", documentsDirectory );
- Return documentsDirectory;
- }
Obtain the Library directory path:
- // Obtain the Library directory
- -(Void) dirLib {
- // [NSHomeDirectory () stringByAppendingPathComponent: @ "Library"];
- NSArray * paths = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES );
- NSString * libraryDirectory = [paths objectAtIndex: 0];
- NSLog (@ "app_home_lib: % @", libraryDirectory );
- }
Obtain the Cache directory path:
- // Obtain the Cache directory
- -(Void) dirCache {
- NSArray * cacPath = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
- NSString * cachePath = [cacPath objectAtIndex: 0];
- NSLog (@ "app_home_lib_cache: % @", cachePath );
- }
Obtain the Tmp directory path:
- // Obtain the Tmp directory
- -(Void) dirTmp {
- // [NSHomeDirectory () stringByAppendingPathComponent: @ "tmp"];
- NSString * tmpDirectory = NSTemporaryDirectory ();
- NSLog (@ "app_home_tmp: % @", tmpDirectory );
- }
Create a folder:
- // Create a folder
- -(Void *) createDir {
- NSString * documentsPath = [self dirDoc];
- NSFileManager * fileManager = [NSFileManager defaultManager];
- NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"];
- // Create a directory
- BOOL res = [fileManager createDirectoryAtPath: testDirectory withIntermediateDirectories: YES attributes: nil error: nil];
- If (res ){
- NSLog (@ "folder created successfully ");
- } Else
- NSLog (@ "Folder creation failed ");
- }
Create a file
- // Create a file
- -(Void *) createFile {
- NSString * documentsPath = [self dirDoc];
- NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"];
- NSFileManager * fileManager = [NSFileManager defaultManager];
- NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"];
- BOOL res = [fileManager createFileAtPath: testPath contents: nil attributes: nil];
- If (res ){
- NSLog (@ "File Created successfully: % @", testPath );
- } Else
- NSLog (@ "file creation failed ");
- }
Write data to a file:
- // Write an object
- -(Void) writeFile {
- NSString * documentsPath = [self dirDoc];
- NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"];
- NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"];
- NSString * content = @ "Test Written content! ";
- BOOL res = [content writeToFile: testPath atomically: YES encoding: NSUTF8StringEncoding error: nil];
- If (res ){
- NSLog (@ "file written successfully ");
- } Else
- NSLog (@ "file write failed ");
- }
Read File data:
- // Read the file
- -(Void) readFile {
- NSString * documentsPath = [self dirDoc];
- NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"];
- NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"];
- // NSData * data = [NSData dataWithContentsOfFile: testPath];
- // NSLog (@ "File Read succeeded: % @", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);
- NSString * content = [NSString stringWithContentsOfFile: testPath encoding: NSUTF8StringEncoding error: nil];
- NSLog (@ "File Read succeeded: % @", content );
- }
File attributes:
- // File attributes
- -(Void) fileAttriutes {
- NSString * documentsPath = [self dirDoc];
- NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"];
- NSFileManager * fileManager = [NSFileManager defaultManager];
- NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"];
- NSDictionary * fileAttributes = [fileManager attributesOfItemAtPath: testPath error: nil];
- NSArray * keys;
- Id key, value;
- Keys = [fileAttributes allKeys];
- Int count = [keys count];
- For (int I = 0; I <count; I ++)
- {
- Key = [keys objectAtIndex: I];
- Value = [fileAttributes objectForKey: key];
- NSLog (@ "Key: % @ for value: % @", key, value );
- }
- }
Delete an object:
- // Delete an object
- -(Void) deleteFile {
- NSString * documentsPath = [self dirDoc];
- NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"];
- NSFileManager * fileManager = [NSFileManager defaultManager];
- NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"];
- BOOL res = [fileManager removeItemAtPath: testPath error: nil];
- If (res ){
- NSLog (@ "File deleted ");
- } Else
- NSLog (@ "file deletion failed ");
- NSLog (@ "does the file exist: % @", [fileManager isExecutableFileAtPath: testPath]? @ "YES": @ "NO ");
- }