A summary of basic methods for accessing and managing file directories in IOS development _ios

Source: Internet
Author: User
Tags parent directory

Access to file directories

Simplest: (Because of the sandbox relationship, no folder concept)

Copy Code code as follows:

uiimage* image = [UIImage imagenamed:@ "11.png"];

This is already a packaged path relative to the app and does not require additional paths.


There is also a need to specify the file path:

Need to use NSBundle.

Copy Code code as follows:

[[NSBundle Mainbundle] ResourcePath], this is the program's packaged path.

If you need to specify a path, write this:

You can also splice yourself:

Copy Code code as follows:

or directly:
Copy Code code as follows:

nsstring* path = [[NSBundle mainbundle] pathforresource:@ "AAA" oftype:@ "PNG"];

Generally, the application has 3 directories

Documents,library,tmp

Now Apple is not allowed to keep large data in documents, if you want to keep the video or something inside documents, need to back up to icould more trouble.

The common practice of the nest is to keep it under the caches directory of the library (don't know if it's reasonable:)

TMP directory, save some temporary files, you can delete the contents of the cache when exiting the program.


Obtain your own documents directory in your application:

Copy Code code as follows:

Nsarray * paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString * documentdirectory = [paths objectatindex:0];

Based on the above, obtain a complete file path and name:
Copy Code code as follows:

NSString * file = [documentdirectory stringbyappendingpathcomponent:@ "file1.txt"];

This allows you to create, read, and write files with file.

File Directory Management
Let's look at some common file directory management methods
1, Common Nsfilemanager file Method

Copy Code code as follows:

-(NSData *) Contentsatpath:path//Read data from a file


-(BOOL) Createfileatpath:path contents: (NSData *) data attributes:attr//write to a file


-(BOOL) Removeitematpath:path error:err//Delete a file


-(BOOL) Moveitematpath:from topath:to error:err//renaming or moving a file (to cannot be existing)

-(BOOL) Copyitematpath:from topath:to error:err//Copy file (to cannot be existing)


-(BOOL) Contentsequalatpath:path andpath:path2//compare contents of two files


-(BOOL) Fileexistatpath:path//test file exists


-(BOOL) Isreadablefileatpath:path//testing whether the file exists and can perform read operations


-(BOOL) Iswriteablefileatpath:path//testing whether a file exists and can perform a write operation


-(Nsdictionary *) Attributesofitematpath:path error:err//Get file properties


-(BOOL) setattributesofitematpath:attr error:err//Change the properties of a file


2. Using the catalogue
Copy Code code as follows:

-(NSString *) Currentdirectorypath//Get current directory


-(BOOL) Changecurrentdirectorypath:path//Change current directory


-(BOOL) Copyitematpath:from topath:to error:err//Copy directory structure (to cannot be existing)


-(BOOL) Createdirectoryatpath:path withintermediatedirectories: (BOOL) flag ATTRIBUTE:ATTR//Create a new directory


-(BOOL) Fileexistatpath:path isdirectory: (bool*) flag//test file is not directory (flag store results yes/no)


-(Nsarray *) Contentsofdirectoryatpath:path Error:err//Listing directory contents


-(Nsdirectoryenumerator *) Enumeratoratpath:path//enumerating contents of directory


-(BOOL) Removeitematpath:path error:err//Delete empty directory


-(BOOL) Moveitematpath:from topath:to error:err//renaming or moving a directory (to cannot be existing)


3, the Common Path tool method
Copy Code code as follows:

+ (NSString *) pathwithcomponens:components//construction of valid paths based on elements in components


-(Nsarray *) pathcomponents//destructor path, get the parts that make up this path


-(NSString *) lastpathcomponent//Extract the last component of the path
-(NSString *) pathextension//To extract its extension from the last part of the path


-(NSString *) Stringbyappendingpathcomponent:path//Add path to the end of an existing path


-(NSString *) Stringbyappendingpathextension:ext//Add the specified extension to the last component of the path

-(NSString *) stringbydeletinglastpathcomponent//Delete the last component of the path


-(NSString *) stringbydeletingpathextension//Remove extension from last part of file

-(NSString *) Stringbyexpandingtileinpath//Extend the character of the path to the user's home directory (~) or the user's home directory (~user)

-(NSString *) Stringbyresolvingsymlinksinpath//attempt to parse symbolic links in the path

-(NSString *) Stringbystandardizingpath//through an attempt to parse ~ 、.. (parent directory symbol),. (current directory symbol) and symbolic links to standardize paths


4, the Common Path tool function
Copy Code code as follows:

nsstring* nsusername (void)//Returns the current user's login name


nsstring* nsfullusername (void)//Returns the full user name of the current user


nsstring* nshomedirectory (void)//Returns the path to the current user's home directory


nsstring* nshomedirectoryforuser (nsstring* user)//Return to user's home directory


nsstring* nstemporarydirectory (void)//Returns a directory of paths that can be used to create temporary files


5, the commonly used iOS directory
Copy Code code as follows:

Documents (NSDocumentDirectory)//for writing to the directory of application-related data files, files written here in iOS can be shared with itunes and accessed, and files stored here are automatically backed up to the cloud

Library/caches (nscachesdirectory)//directory for writing application support files, saving the information that the application needs to start again. itunes does not back up the contents of this directory

TMP (use Nstemporarydirectory ())///This directory is used for temporary files, which need to be removed only when the program terminates, and should be removed from this directory when the application no longer needs these temporary files

Library/preferences//This directory contains the application's preferences file, using the Nsuserdefault class to create, read, and modify the preferences file


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.