iOS managing files and directories

Source: Internet
Author: User
<span id="Label3"></p>The Nsfilemaneger File Manager provides many common file operations that users can manage by using shared file Management Objects (nsfilemanager *FM = [nsfilemanager defaultmanager];); iOS and Mac OS X After 10.5, the user can create a unique File Manager delegate Object. File Manager can be used to locate and create copies of moving files and Folders. Gets the file or folder properties (size, modification date, Permissions) and can also be used to change the properties of files and Folders. File Manager supports Nsurl and nsstring two ways to locate files or Folders. Nsurl is usually the preferred way, because it transforms the path information into a more efficient representation. You can also get a "bookmark" from nsurl, an alias-like way to provide a way to locate a file or Folder. If you want to move, copy, link, or delete files or folders, you can use a file Manager's agent to manage these operations. Proxies are used to define these operations and how to handle them after an Error. After Mac OS X 10.7, the agent must conform to the Nsfilemanagerdelegate Protocol. After IOS 5.0 and Mac OS X 10.7, Nsfilemanager introduced a number of new ways to store files in Icloud. The file tags and folder labels used to store in the cloud and icloud should be Synchronized. This allows the file to be shared between the User's iOS device and the Mac Device. Changes to files or folders in one place are sent to other devices to determine the synchronization of these Changes.<p><p>Nsfilemanager shared objects are Thread-safe. But if you use the Proxy method to receive the file operation message, you need to create a Single-instance file management object to the agent, and use the object to complete the file operation, to ensure thread Safety.</p></p><p><p>1. Common Nsfilemanager File methods</p></p><p><p>-(nsdata *) contentsatpath:path//read data from a file</p></p><p><p>-(BOOL) createfileatpath:path contents: (nsdata *) data attributes:attr//write to a file</p></p><p><p>-(BOOL) Removeitematpath:path error:err//delete a file</p></p><p><p>-(BOOL) moveitematpath:from topath:to error:err//rename or move a file (to cannot be Existing)</p></p><p><p>-(BOOL) copyitematpath:from topath:to error:err//copy file (to cannot be Existing)</p></p><p><p>-(BOOL) Contentsequalatpath:path Andpath:path2//compare The contents of two files</p></p><p><p>-(BOOL) fileexistatpath:path//test file exists</p></p><p><p>-(BOOL) isreadablefileatpath:path//test file exists and can perform read operations</p></p><p><p>-(BOOL) iswriteablefileatpath:path//test file exists, and can perform write operations</p></p><p><p>-(nsdictionary *) Attributesofitematpath:path Error:err//get The properties of the file</p></p><p><p>-(BOOL) setattributesofitematpath:attr Error:err//change The properties of a file</p></p><p><p>2. Using the Catalog</p></p><p><p>-(nsstring *) currentdirectorypath//get Current directory</p></p><p><p>-(BOOL) Changecurrentdirectorypath:path//change The current directory</p></p><p><p>-(BOOL) copyitematpath:from topath:to error:err//copy directory structure (to cannot be Existing)</p></p><p><p>-(bool) createdirectoryatpath:path withintermediatedirectories: (bool) flag attribute:attr//create A new directory</p></p><p><p>-(BOOL) fileexistatpath:path isdirectory: (bool*) flag//test file is not a directory (flag stored results yes/no)</p></p><p><p>-(nsarray *) Contentsofdirectoryatpath:path error:err//list Contents</p></p><p><p>-(nsdirectoryenumerator *) enumeratoratpath:path//enumerate The contents of the directory</p></p><p><p>-(BOOL) removeitematpath:path error:err//delete Empty Directory</p></p><p><p>-(BOOL) moveitematpath:from topath:to error:err//rename or move a directory (to cannot be Existing)</p></p><p><p>3. Common Path Tool method</p></p><p><p>+ (nsstring *) pathwithcomponens:components//construct A valid path based on elements in components</p></p><p><p>-(nsarray *) pathcomponents//destructor path for each part that makes up this path</p></p><p><p>-(nsstring *) lastpathcomponent//extract The last component of the path</p></p><p><p>-(nsstring *) pathextension//extract Its extension from the last component of the path</p></p><p><p>-(nsstring *) Stringbyappendingpathcomponent:path//add Path to the end of an existing path</p></p><p><p>-(nsstring *) stringbyappendingpathextension:ext//add The specified extension to the last component of the path</p></p><p><p>-(nsstring *) Stringbydeletinglastpathcomponent//delete The last component of the path</p></p><p><p>-(nsstring *) stringbydeletingpathextension//remove The extension from the last part of the file</p></p><p><p>-(nsstring *) Stringbyexpandingtileinpath//expand The path to the user home directory (~) or the home directory of the specified user (~user)</p></p><p><p>-(nsstring *) Stringbyresolvingsymlinksinpath//try to parse the symbolic link in the path</p></p><p><p>-(nsstring *) stringbystandardizingpath//by trying to parse ~ 、.. (parent directory symbol),. (current directory symbol), and symbolic link to standardize the path</p></p><p><p>4. Common Path Tool functions</p></p><p><p>nsstring* nsusername (void)//returns The login name of the current user</p></p><p><p>nsstring* Nsfullusername (void)//returns The full user name of the current user</p></p><p><p>nsstring* nshomedirectory (void)//returns The path of the current user home directory</p></p><p><p>nsstring* nshomedirectoryforuser (nsstring* User)//return to User's home directory</p></p><p><p>nsstring* nstemporarydirectory (void)//returns The path directory that can be used to create temporary files</p></p><p><p>5. Common iOS Directory</p></p><p><p>Documents (nsdocumentdirectory)//directory for writing application-related data files, files written here in iOS can be shared with itunes and accessed, files stored here are automatically backed up to the cloud</p></p><p><p>Library/caches (nscachesdirectory)///directory for writing application support files, Save the information that the application needs to start Again. itunes does not back up the contents of this directory</p></p><p><p>TMP (use nstemporarydirectory ())//this directory is used to store temporary files, only the program terminates the need to remove these files, when the application no longer needs these temporary files, should be removed from this directory</p></p><p><p>Library/preferences//this directory contains the Application's preferences file, using the Nsuserdefault class to create, read and modify the preferences file</p></p><p><p></p></p><p><p>iOS managing files and directories</p></p></span>

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.