IOS File Operations

Source: Internet
Author: User

1. Common nsfilemanager file Methods

-(Nsdata *) contentsatpath: path // read data from a file

-(Bool) createfileatpath: path contents :( nsdata *) data attributes: ATTR // write data to a file

-(Bool) removeitematpath: path error: Err // delete an object

-(Bool) moveitematpath: From topath: To error: Err // rename or move a file (to cannot be an existing one)

-(Bool) copyitematpath: From topath: To error: Err // copy a file (to cannot be an existing one)

-(Bool) contentsequalatpath: path andpath: path2 // compare the content of the two files

-(Bool) fileexistatpath: path // test whether the file exists

-(Bool) isreadablefileatpath: path // test whether the file exists and whether the file can be read

-(Bool) iswriteablefileatpath: path // test whether the file exists and whether the write operation can be performed

-(Nsdictionary *) attributesofitematpath: path error: Err // get the Object Attributes

-(Bool) setattributesofitematpath: ATTR error: Err // modify the file attributes

2. Use the Directory

-(Nsstring *) currentdirectorypath // obtain the current directory

-(Bool) changecurrentdirectorypath: path // change the current directory

-(Bool) copyitematpath: From topath: To error: Err // copy the directory structure (to cannot be an existing one)

-(Bool) createdirectoryatpath: path withintermediatedirectories :( bool) Flag attribute: ATTR // create a new directory

-(Bool) fileexistatpath: path isdirectory :( bool *) Flag // test whether the file is a directory (the result stored in the flag is yes/no)

-(Nsarray *) contentsofdirectoryatpath: path error: Err // list directory content

-(Nsdirectoryenumerator *) enumeratoratpath: path // content of the enumerated directory

-(Bool) removeitematpath: path error: Err // delete an empty directory

-(Bool) moveitematpath: From topath: To error: Err // rename or move a directory (to cannot be an existing Directory)

3. Common Path tools and methods

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

-(Nsarray *) pathcomponents // The Destructor path to obtain each part of the path

-(Nsstring *) lastpathcomponent // extract the last part of the path

-(Nsstring *) pathextension // extract the extension from the last component of the path

-(Nsstring *) stringbyappendingpathcomponent: path // Add the path to the end of the existing path

-(Nsstring *) stringbyappendingpathextension: ext // Add the specified extension to the last component of the path.

-(Nsstring *) stringbydeletinglastpathcomponent // Delete the last part of the path

-(Nsstring *) stringbydeletingpathextension // Delete the extension from the last part of the file

-(Nsstring *) stringbyexpandingtileinpath // extend the character in the path to the user's main directory (~) Or specify the user's main directory (~ User)

-(Nsstring *) stringbyresolvingsymlinksinpath // try to parse the symbolic link in the path

-(Nsstring *) stringbystandardizingpath // try to parse ~ ,. (Parent directory symbol),. (current directory symbol) and symbolic link to standardize the path

4. Common Path tool Functions

Nsstring * nsusername (void) // return the login name of the current user

Nsstring * nsfullusername (void) // return the complete user name of the current user

Nsstring * nshomedirectory (void) // return the path of the current user's home directory

Nsstring * nshomedirectoryforuser (nsstring * User) // return the Home Directory of the user

Nsstring * nstemporarydirectory (void) // return the path directory that can be used to create a temporary file

5. Common IOS Directories

Documents (nsdocumentdirectory) // directory used to write application-related data files. Files written here in IOS can be shared and accessed with iTunes. files stored here are automatically backed up to the cloud.

Library/caches (nscachesdirectory) // used to write the directory of the application support file and save the information required for the application to start again. ITunes will not back up the contents of this directory

TMP (use nstemporarydirectory () // This directory is used to store temporary files. You only need to remove these files when the program ends. When the application no longer needs these temporary files, delete it from this directory

Library/preferences // This directory contains the preferences file of the application. Use the nsuserdefault class to create, read, and modify preferences.

For example, deleting all files in the tmp directory


Nsstring * imagedir = [nsstring stringwithformat: @ "% @", nstemporarydirectory ()];

Nsfilemanager * filemanager = [nsfilemanager defaultmanager];
Nsarray * array = [filemanager contentsofdirectoryatpath: imagedir error: Nil];
Nslog (@ "% @", [filemanager contentsofdirectoryatpath: imagedir error: Nil]);
For (Int J = 0; j <[array count]; j ++ ){
[Filemanager removeitematpath: [nsstring stringwithformat: @ "% @", imagedir, [array objectatindex: J] error: Nil];
}

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.