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