NSFileManager for iPhone File System

Source: Internet
Author: User

IPhone File System NSFileManagerThe description is the content to be introduced in this article, mainly throughIphone File SystemTo learnNSFileManagerFor more information, see this document.

IPhone File System: create, rename, and delete files. NSFileManager contains methods for querying single dictionary directories, creating, renaming, and deleting directories, and obtaining/setting file attributes for readability, programming ).

Each program has its own sandbox, through which you can read/write files. The files written into the sandbox will remain stable in the Process of the program, even if the program is updated.

You can locate the file directory in the sandbox as follows:

 
 
  1. // For error messages
  2. NSError * error;
  3. // Create a file manager
  4. NSFileManager * fileMgr = [NSFileManagerdefaultManager];
  5. // Point to the file directory
  6. NSString * documentsDirectory = [NSHomeDirectory ()
  7. StringByAppendingPathComponent: @ "Documents"];
  8.  
  9. // Create a directory
  10. [[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat: @ "% @/myFolder", NSHomeDirectory ()] attributes: nil];

Create a file

Now that we have a file directory, we can use this path to create a new file in the sandbox and write a piece of code:

 
 
  1. // File we want to create in the specified ents directory the File we want to create will appear in the File directory
  2. // Result is:/Documents/file1.txt. The Result is/Documents/file1.txt.
  3. NSString * filePath = [documentsDirectory
  4. StringByAppendingPathComponent: @ "file1.txt"];
  5. // String to be written
  6. NSString * str = @ "iPhoneDeveloper Tips \ nhttp: // iPhoneDevelopTips, com ";
  7. // Write a file
  8. [Str writeToFile: filePath atomically: YES
  9. Encoding: NSUTF8StringEncoding error: & error];
  10. // Display the contents of the file directory
  11. NSLog (@ "Documentsdirectory: % @",
  12. [FileMgr contentsOfDirectoryAtPath: documentsDirectoryerror: & error]);

Create a program file1.txt for the desired file), initialize a string to write the file, and list the directories. The last line shows a directory list that appears in the file directory after the file is created:

Rename a file

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.

 
 
  1. // Rename the object by moving the object
  2. NSString * filePath2 = [documentsDirectory
  3. StringByAppendingPathComponent: @ "file2.txt"];
  4. // Determine whether to move
  5. If ([fileMgr moveItemAtPath: filePath toPath: filePath2 error: & error]! = YES)
  6. NSLog (@ "Unable to move file: % @", [error localizedDescription]);
  7. // Display the contents of the file directory
  8. NSLog (@ "Documentsdirectory: % @",
  9. [FileMgr contentsOfDirectoryAtPath: documentsDirectoryerror: & error]);

After the file is moved, the output result is shown in:

Delete an object

To complete this technique, let's take a look at how to delete an object:

 
 
  1. // Determine whether to delete the file in filePath2
  2. If ([fileMgr removeItemAtPath: filePath2 error: & error]! = YES)
  3. NSLog (@ "Unable to delete file: % @", [error localizedDescription]);
  4. // Display the contents of the file directory
  5. NSLog (@ "Documentsdirectory: % @",
  6. [FileMgr contentsOfDirectoryAtPath: documentsDirectoryerror: & error]);

Once the file is deleted, as you expected, the file directory will be automatically cleared:

These examples can only show you some details about file processing. To get a more comprehensive and detailed explanation, you need to understand the NSFileManager file.

When developing an iPhone program, you sometimes need to perform operations on files. Obtaining the list of all files in a directory is one of the basic operations. The following code retrieves a list of files and folders in a directory.

 
 
  1. NSFileManager * fileManager = [NSFileManager defaultManager];
  2. // Obtain the list of files and folders in the application Documents folder.
  3. NSArray * documentPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
  4. NSString * documentDir = [documentPaths objectAtIndex: 0];
  5. NSError * error = nil;
  6. NSArray * fileList = [[NSArray alloc] init];
  7. // FileList is an array containing the file names and folder names of all files in the folder.
  8. FileList = [fileManager contentsOfDirectoryAtPath: documentDir error: & error];

The following code lists the names of all subfolders in a given folder.

 
 
  1. NSMutableArray * dirArray = [[NSMutableArray alloc] init];
  2. BOOL isDir = NO;
  3. // List the folder names in the fileList obtained in the above section.
  4. For (NSString * file in fileList ){
  5. NSString * path = [documentDir stringByAppendingPathComponent: file];
  6. [FileManager fileExistsAtPath: path isDirectory :( & isDir)];
  7. If (isDir ){
  8. [DirArray addObject: file];
  9. }
  10. IsDir = NO;
  11. }
  12. NSLog (@ "Every Thing in the dir: % @", fileList );
  13. NSLog (@ "All folders: % @", dirArray );

Summary: AboutIPhone File System NSFileManagerI hope this article will be helpful to you!

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.