Objective-c Nsfilemanager File Management summary

Source: Internet
Author: User

Createfileatpath//Create File

        Nsfilemanager *FM = [Nsfilemanager Defaultmanager];        NSString *strpath = [NSString stringwithformat:@ "%@/file1.txt", Nshomedirectory ()];        NSString *strdata = @ "Test";                BOOL BRET = [FM createfileatpath:strpath contents:strdata Attributes:nil];        if (!bret)        {            NSLog (@ "Create file Error");        }


Copyitematpath//Copy files

        Nsfilemanager *FM = [Nsfilemanager Defaultmanager];        NSString *strpath1 = [NSString stringwithformat:@ "%@/file1.txt", Nshomedirectory ()];        NSString *strpath2 = [NSString stringwithformat:@ "%@/file2.txt", Nshomedirectory ()];                BOOL BRET = [FM copyitematpath:strpath1 topath:strpath2 Error:nil];        if (!bret)        {            NSLog (@ "Copy file Error");        }

Moveitematpath//Moving files

        Nsfilemanager *FM = [Nsfilemanager Defaultmanager];        NSString *strpath1 = [NSString stringwithformat:@ "%@/file1.txt", Nshomedirectory ()];        NSString *strpath2 = [NSString stringwithformat:@ "%@/file2.txt", Nshomedirectory ()];                BOOL BRET = [FM moveitematpath:strpath1 topath:strpath2 Error:nil];        if (!bret)        {            NSLog (@ "Move file error");        }

removeItemAtPath//delete files

        Nsfilemanager *FM = [Nsfilemanager Defaultmanager];        NSString *strpath1 = [NSString stringwithformat:@ "%@/file1.txt", Nshomedirectory ()];                BOOL BRET = [FM removeitematpath:strpath1 Error:nil];        if (!bret)        {            NSLog (@ "Delete file error");        }


Attributesofitematpath//Get file attributes, file characters, return nsdictionary

        Nsfilemanager *FM = [Nsfilemanager Defaultmanager];        NSString *strpath1 = [NSString stringwithformat:@ "%@/log.txt", Nshomedirectory ()];                Nsdictionary *dic = [FM attributesofitematpath:strpath1 Error:nil];        NSLog (@ "%@", DIC);

Currentdirectorypath//Get current directory

       Nsfilemanager *FM = [Nsfilemanager Defaultmanager];       NSString *STRPATH = [FM Currentdirectorypath];       NSLog (@ "%@", strpath);

Createdirectoryatpath//Create a directory

        Nsfilemanager *FM = [Nsfilemanager Defaultmanager];        NSString *strpath1 = [NSString stringwithformat:@ "%@/testdir", Nshomedirectory ()];        BOOL BRET = [FM createdirectoryatpath:strpath1 withintermediatedirectories:no Attributes:nil Error:nil];        if (!bret)        {            NSLog (@ "Create dir error");        }

Fileexistsatpath//Determine if a file or directory exists

        Nsfilemanager *FM = [Nsfilemanager Defaultmanager];        NSString *strpath1 = [NSString stringwithformat:@ "%@/testdir", Nshomedirectory ()];        BOOL BRET = [FM fileexistsatpath:strpath1];        if (!bret)        {            NSLog (@ "no file exist");        }        else        {            NSLog (@ "file exist");        }


Enumeratoratpath//Enumerate directories, enumerate subdirectories all

        Nsfilemanager *FM = [Nsfilemanager Defaultmanager];        NSString *strpath1 = [NSString stringwithformat:@ "%@/desktop", Nshomedirectory ()];        Nsdirectoryenumerator *dirs = [FM enumeratoratpath:strpath1];                NSString *dir;        while (Dir=[dirs Nextobject]) {            NSLog (@ "%@", dir);        }

Contentsofdirectoryatpath//Enumerate directories, do not enumerate subdirectories

        Nsfilemanager *FM = [Nsfilemanager Defaultmanager];        NSString *strpath1 = [NSString stringwithformat:@ "%@/desktop", Nshomedirectory ()];        Nsarray *dirs = [FM contentsofdirectoryatpath:strpath1 Error:nil];                NSString *dir;        For (dir in dirs)        {            NSLog (@ "%@", dir);        }


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Objective-c Nsfilemanager File Management summary

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.