Methods and Nsfilemanager classes for obtaining directory paths for individual files in iOS

Source: Internet
Author: User
Tags tmp folder

Transferred from: http://blog.sina.com.cn/s/blog_5fb39f910101di92.html

ways to get directory paths for various files in iOS

The iphone sandbox model has four folders, what is the location of the permanent data store, and what is the simple way to get the path to the emulator.

Documents,tmp,app,library.

(Nshomedirectory ()),

The manually saved files are in the documents file.

Nsuserdefaults saved files in the TMP folder

1. Documents directory: You should write all the de application data files to this directory. This directory is used to store user data or other information that should be backed up regularly.

2. Appname.app directory: This is the application's package directory, which contains the application itself. Because the application must be signed, you cannot modify the contents of this directory at run time, which may cause the application to fail to start.

3. Library directory: There are two sub-directories under this directory: Caches and Preferences
Preferences directory: Contains the application's preferences file. Instead of creating a preference file directly, you should use the Nsuserdefaults class to get and set your application's preferences.
Caches directory: Used to hold application-specific support files, saving the information that is needed during application startup.

4. tmp directory: This directory is used to store temporary files, saving information that is not needed during the application restart.


Ways to get these directory paths:
1, get the Home directory path function:
NSString *homedir = Nshomedirectory ();
2. Method for obtaining the Documents directory path:
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *docdir = [Paths objectatindex:0];
3, get the Caches directory path method:
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);
NSString *cachesdir = [Paths objectatindex:0];
4. Method to get the TMP directory path:
NSString *tmpdir = Nstemporarydirectory ();
5, gets the method of the resource file path in the application package:
For example, get a picture resource (apple.png) path in the package by:
NSString *imagepath = [[NSBundle mainbundle] pathforresource:@ "apple" oftype:@ "PNG"];
UIImage *appleimage = [[UIImage alloc] initwithcontentsoffile:imagepath];
The Mainbundle class method in the code is used to return an object that represents the application package.

several ways to get the contents of the iphone Sandbox (sandbox): [CPP]View Plaincopyprint?
    1. Get the Sandbox home directory path
    2. NSString *homedir = Nshomedirectory ();
    3. Get the Documents directory path
    4. Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
    5. NSString *docdir = [Paths objectatindex:0];
    6. Get caches directory path
    7. Nsarray *paths = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);
    8. NSString *cachesdir = [Paths objectatindex:0];
    9. Get the TMP directory path
    10. NSString *tmpdir = Nstemporarydirectory ();
[CPP]View Plaincopyprint?
    1. Gets a picture resource (apple.png) path in the current package
    2. nsstring *imagepath = [[NSBundle mainbundle] pathforresource:@"Apple" oftype:@"PNG" ];
    3. UIImage *appleimage = [[UIImage alloc] initwithcontentsoffile:imagepath];

Example:

nsfilemanager* Fm=[nsfilemanager Defaultmanager];
if (![ FM fileexistsatpath:[self DataFilePath]) {

The following is a save on the path to the file development
[FM createdirectoryatpath:[self DataFilePath] Withintermediatedirectories:yes attributes:nil Error:nil];

Get all the filenames in a directory
Nsarray *files = [FM subpathsatpath: [self datafilepath]];

Read a file
NSData *data = [FM contentsatpath:[self DataFilePath];

Or
NSData *data = [NSData datawithcontentofpath:[self DataFilePath];
}

iOS managing files and directories

1. Common Nsfilemanager File methods

-(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//rename or move 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 the contents of two files

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

-(BOOL) Isreadablefileatpath:path//test file exists and can perform read operations

-(BOOL) Iswriteablefileatpath:path//test file exists, and can perform write operations

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

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

2. Using the Catalog

-(NSString *) Currentdirectorypath//Get current directory

-(BOOL) Changecurrentdirectorypath:path//Change the 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 a directory (flag stored results yes/no)

-(Nsarray *) Contentsofdirectoryatpath:path error:err//List Contents

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

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

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

3. Common Path Tool method

+ (NSString *) pathwithcomponens:components//construct a valid path based on elements in components

-(Nsarray *) pathcomponents//destructor path for each part that makes up this path

-(NSString *) lastpathcomponent//Extract the last component of the path

-(NSString *) pathextension//extract its extension from the last component 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 the extension from the last part of the file

-(NSString *) Stringbyexpandingtileinpath//Expand the path to the user home directory (~) or the home directory of the specified user (~user)

-(NSString *) Stringbyresolvingsymlinksinpath//try to parse the symbolic link in the path

-(NSString *) Stringbystandardizingpath//by trying to parse ~ 、.. (parent directory symbol),. (current directory symbol), and symbolic link to standardize the path

4. Common Path Tool functions

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

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

nsstring* nshomedirectory (void)//Returns the path of the current user home directory

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

nsstring* nstemporarydirectory (void)//Returns the path directory that can be used to create temporary files

5. Common iOS Directory

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

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

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

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

Methods and Nsfilemanager classes for obtaining directory paths for individual files in iOS

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.