Transferred from: http://blog.csdn.net/ios_che/article/details/7287266
iphone File system Nsfilemanager explanation is the article to introduce the content, mainly through the iphone file system to learn how to use the Nsfilemanager , the specific content of this article is detailed.
iphone file system: Create, rename, and delete files, Nsfilemanager contains methods for querying the Word library directory, creating, renaming, deleting directories, and getting/setting file properties (readability, authoring, and so on).
Each program will have its own sandbox, through which you can read/write files. Files written to the sandbox will remain stable in the process of the program, even if the actual program is updated.
As shown below, you can locate the file directory in the sandbox:
- For error messages
- Nserror *error;
- Creating a File Manager
- Nsfilemanager *filemgr = [Nsfilemanagerdefaultmanager];
- Point to 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 the 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 Documents directory the file that we want to create will appear in the file directory
- // result is: /documents/ File1.txt result:/documents/file1.txt
- nsstring *filepath= [documentsdirectory
- stringbyappendingpathcomponent:@ "file1.txt"];
- //the string to be written
- nsstring *str= @ " Iphonedeveloper tips\nhttp://iphonedeveloptips,com ";
- //write file
- [Str writetofile:filepath atomically:yes
- encoding:nsutf8stringencoding error:&error];
- //Displays the contents of the file directory
- nslog (@ "documentsdirectory: %@",
- [ Filemgr contentsofdirectoryatpath:documentsdirectoryerror:&error]);
We build a path (file1.txt) for the file we want to create, initialize a string to write to the file, and list the directories. The last line shows a list of directories that appear in the file directory after we create the file:
Renaming a file
To rename a file, we need to move the file to a new path. The following code creates the path to the target file that we expect, and then requests that the file be moved and the file directory displayed after the move.
- Rename a file by moving the file
- 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]);
- Displaying the contents of a file directory
- NSLog (@ "documentsdirectory:%@",
- [Filemgr Contentsofdirectoryatpath:documentsdirectoryerror:&error]);
After the file has been moved, the output should look like the following:
Delete a file
To make this technique complete, let's look at how to delete a file:
- Determine whether to delete this file in FilePath2
- if ([Filemgr removeitematpath:filepath2 error:&error]! = YES)
- NSLog (@ "Unable to delete file:%@", [Error localizeddescription]);
- Displaying the contents of a file directory
- NSLog (@ "documentsdirectory:%@",
- [Filemgr Contentsofdirectoryatpath:documentsdirectoryerror:&error]);
Once the file has been deleted, as you might expect, the file directory will be automatically emptied:
These examples can teach you only some of the fur on file processing. To get a more comprehensive and detailed explanation, you need to master the knowledge of Nsfilemanager files.
When developing an iphone program, sometimes you need to do something about it. Getting a list of all the files in a directory is one of the basic operations. With this code, you can get a list of files and folders within a directory.
- Nsfilemanager *FileManager = [Nsfilemanager Defaultmanager];
- Get the list of files and folders in the Application Documents folder here
- Nsarray *documentpaths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES);
- NSString *documentdir = [Documentpaths objectatindex:0];
- Nserror *error = nil;
- Nsarray *fileList = [[Nsarray alloc] init];
- FileList is an array of file names and folder names that contain all the files under that folder
- fileList = [FileManager contentsofdirectoryatpath:documentdir error:&error];
The following code can list all the subfolder names in a given folder
- Nsmutablearray *Dirarray = [[Nsmutablearray alloc] init];
- BOOL isdir = NO;
- The folder name is listed in the FileList obtained in the above program
- 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: About the iphone file system Nsfilemanager explained the content of the introduction, hope that through this study can help you!
iOS Nsfilemanager usage detailed