IOS learning notes-File Operations (NSFileManager) and iosnsfilemanager

Source: Internet
Author: User

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:
  1. -(Void) dirHome {
  2. NSString * dirHome = NSHomeDirectory ();
  3. NSLog (@ "app_home: % @", dirHome );
  4. }

Obtain the Documents directory path:

 

  1. // Obtain the Documents directory
  2. -(NSString *) dirDoc {
  3. // [NSHomeDirectory () stringByAppendingPathComponent: @ "events"];
  4. NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
  5. NSString * documentsDirectory = [paths objectAtIndex: 0];
  6. NSLog (@ "app_home_doc: % @", documentsDirectory );
  7. Return documentsDirectory;
  8. }

Obtain the Library directory path:

 

  1. // Obtain the Library directory
  2. -(Void) dirLib {
  3. // [NSHomeDirectory () stringByAppendingPathComponent: @ "Library"];
  4. NSArray * paths = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES );
  5. NSString * libraryDirectory = [paths objectAtIndex: 0];
  6. NSLog (@ "app_home_lib: % @", libraryDirectory );
  7. }

Obtain the Cache directory path:
  1. // Obtain the Cache directory
  2. -(Void) dirCache {
  3. NSArray * cacPath = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
  4. NSString * cachePath = [cacPath objectAtIndex: 0];
  5. NSLog (@ "app_home_lib_cache: % @", cachePath );
  6. }

Obtain the Tmp directory path:
  1. // Obtain the Tmp directory
  2. -(Void) dirTmp {
  3. // [NSHomeDirectory () stringByAppendingPathComponent: @ "tmp"];
  4. NSString * tmpDirectory = NSTemporaryDirectory ();
  5. NSLog (@ "app_home_tmp: % @", tmpDirectory );
  6. }

Create a folder:
  1. // Create a folder
  2. -(Void *) createDir {
  3. NSString * documentsPath = [self dirDoc];
  4. NSFileManager * fileManager = [NSFileManager defaultManager];
  5. NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"];
  6. // Create a directory
  7. BOOL res = [fileManager createDirectoryAtPath: testDirectory withIntermediateDirectories: YES attributes: nil error: nil];
  8. If (res ){
  9. NSLog (@ "folder created successfully ");
  10. } Else
  11. NSLog (@ "Folder creation failed ");
  12. }


Create a file
  1. // Create a file
  2. -(Void *) createFile {
  3. NSString * documentsPath = [self dirDoc];
  4. NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"];
  5. NSFileManager * fileManager = [NSFileManager defaultManager];
  6. NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"];
  7. BOOL res = [fileManager createFileAtPath: testPath contents: nil attributes: nil];
  8. If (res ){
  9. NSLog (@ "File Created successfully: % @", testPath );
  10. } Else
  11. NSLog (@ "file creation failed ");
  12. }

Write data to a file:
  1. // Write an object
  2. -(Void) writeFile {
  3. NSString * documentsPath = [self dirDoc];
  4. NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"];
  5. NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"];
  6. NSString * content = @ "Test Written content! ";
  7. BOOL res = [content writeToFile: testPath atomically: YES encoding: NSUTF8StringEncoding error: nil];
  8. If (res ){
  9. NSLog (@ "file written successfully ");
  10. } Else
  11. NSLog (@ "file write failed ");
  12. }

Read File data:
  1. // Read the file
  2. -(Void) readFile {
  3. NSString * documentsPath = [self dirDoc];
  4. NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"];
  5. NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"];
  6. // NSData * data = [NSData dataWithContentsOfFile: testPath];
  7. // NSLog (@ "File Read succeeded: % @", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);
  8. NSString * content = [NSString stringWithContentsOfFile: testPath encoding: NSUTF8StringEncoding error: nil];
  9. NSLog (@ "File Read succeeded: % @", content );
  10. }

File attributes:
  1. // File attributes
  2. -(Void) fileAttriutes {
  3. NSString * documentsPath = [self dirDoc];
  4. NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"];
  5. NSFileManager * fileManager = [NSFileManager defaultManager];
  6. NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"];
  7. NSDictionary * fileAttributes = [fileManager attributesOfItemAtPath: testPath error: nil];
  8. NSArray * keys;
  9. Id key, value;
  10. Keys = [fileAttributes allKeys];
  11. Int count = [keys count];
  12. For (int I = 0; I <count; I ++)
  13. {
  14. Key = [keys objectAtIndex: I];
  15. Value = [fileAttributes objectForKey: key];
  16. NSLog (@ "Key: % @ for value: % @", key, value );
  17. }
  18. }

Delete an object:

  1. // Delete an object
  2. -(Void) deleteFile {
  3. NSString * documentsPath = [self dirDoc];
  4. NSString * testDirectory = [documentsPath stringByAppendingPathComponent: @ "test"];
  5. NSFileManager * fileManager = [NSFileManager defaultManager];
  6. NSString * testPath = [testDirectory stringByAppendingPathComponent: @ "test.txt"];
  7. BOOL res = [fileManager removeItemAtPath: testPath error: nil];
  8. If (res ){
  9. NSLog (@ "File deleted ");
  10. } Else
  11. NSLog (@ "file deletion failed ");
  12. NSLog (@ "does the file exist: % @", [fileManager isExecutableFileAtPath: testPath]? @ "YES": @ "NO ");
  13. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.