Introduction to iOS file processing
File processing cannot be interpreted intuitively through the application, and we can learn about the file processing of iOS from the following examples.
Action on the file in iOS. Because the application is in the sandbox (sandbox), the file read and write permissions are restricted, only a few directories to read and write files.
Methods used in file processing
The list of methods used to access and manipulate files is listed below.
The following instance you must replace the FILEPATH1, filepath, and filepath strings for the full file path to get the desired action.
Check if the file exists
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; Get documents directory Nsarray *directorypaths = Nssearchpathfordirectoriesindomains ( NSDocumentDirectory, Nsuserdomainmask, YES); NSString *documentsdirectorypath = [directorypaths objectatindex:0]; if ([FileManager fileexistsatpath:@ ""]==yes) { NSLog (@ "File exists"); }
Compare the contents of two files
if ([FileManager contentsequalatpath:@ "FilePath1" andpath:@ "FilePath2"]) { NSLog (@ "Same content"); }
Check for writable, readable, executable files
if ([FileManager iswritablefileatpath:@ "FilePath"]) { NSLog (@ "iswritable"); } if ([FileManager isreadablefileatpath:@ "FilePath"]) { NSLog (@ "isreadable"); } if ([FileManager isexecutablefileatpath:@ "FilePath"]) { NSLog (@ "is executable"); }
Moving files
if ([FileManager moveitematpath:@ "FilePath1" topath:@ "FilePath2" Error:null]) { NSLog (@ "Moved Successfully "); }
Copying files
if ([FileManager copyitematpath:@ "FilePath1" topath:@ "FilePath2" Error:null]) { NSLog (@ "Copied Successfully "); }
deleting files
if ([FileManager removeitematpath:@ "FilePath" Error:null]) { NSLog (@ "removed successfully");
Read file
Write file
[FileManager createfileatpath:@ "" Contents:data Attributes:nil];