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:
- // For error messages
- NSError * error;
- // Create a file manager
- NSFileManager * fileMgr = [NSFileManagerdefaultManager];
- // Point to the file directory
- NSString * documentsDirectory = [NSHomeDirectory ()
- StringByAppendingPathComponent: @ "Documents"];
-
- // Create a directory
- [[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:
- // File we want to create in the specified ents directory the File we want to create will appear in the File directory
- // Result is:/Documents/file1.txt. The Result is/Documents/file1.txt.
- NSString * filePath = [documentsDirectory
- StringByAppendingPathComponent: @ "file1.txt"];
- // String to be written
- NSString * str = @ "iPhoneDeveloper Tips \ nhttp: // iPhoneDevelopTips, com ";
- // Write a file
- [Str writeToFile: filePath atomically: YES
- Encoding: NSUTF8StringEncoding error: & error];
- // Display the contents of the file directory
- NSLog (@ "Documentsdirectory: % @",
- [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.
- // Rename the object by moving the object
- NSString * filePath2 = [documentsDirectory
- StringByAppendingPathComponent: @ "file2.txt"];
- // Determine whether to move
- If ([fileMgr moveItemAtPath: filePath toPath: filePath2 error: & error]! = YES)
- NSLog (@ "Unable to move file: % @", [error localizedDescription]);
- // Display the contents of the file directory
- NSLog (@ "Documentsdirectory: % @",
- [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:
- // Determine whether to delete the file in filePath2
- If ([fileMgr removeItemAtPath: filePath2 error: & error]! = YES)
- NSLog (@ "Unable to delete file: % @", [error localizedDescription]);
- // Display the contents of the file directory
- NSLog (@ "Documentsdirectory: % @",
- [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.
- NSFileManager * fileManager = [NSFileManager defaultManager];
- // Obtain the list of files and folders in the application Documents folder.
- NSArray * documentPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
- NSString * documentDir = [documentPaths objectAtIndex: 0];
- NSError * error = nil;
- NSArray * fileList = [[NSArray alloc] init];
- // FileList is an array containing the file names and folder names of all files in the folder.
- FileList = [fileManager contentsOfDirectoryAtPath: documentDir error: & error];
The following code lists the names of all subfolders in a given folder.
- NSMutableArray * dirArray = [[NSMutableArray alloc] init];
- BOOL isDir = NO;
- // List the folder names in the fileList obtained in the above section.
- For (NSString * file in fileList ){
- NSString * path = [documentDir stringByAppendingPathComponent: file];
- [FileManager fileExistsAtPath: path isDirectory :( & isDir)];
- If (isDir ){
- [DirArray addObject: file];
- }
- IsDir = NO;
- }
- NSLog (@ "Every Thing in the dir: % @", fileList );
- NSLog (@ "All folders: % @", dirArray );
Summary: AboutIPhone File System NSFileManagerI hope this article will be helpful to you!