Objective-c file and directory operations, iOS file operations, Nsfilemanager using file operations:
Objective-c uses the Nsfilemanager class to manage and manipulate files, directories, Nsfilemanager, files, or directories that are unique to the path name of the file used. Each pathname is a NSString object.
Nsfilemanager objects create instances by using the Defaultmanager method
Gca
Nsfilemanager *FM = [Nsfilemanager Defaultmanager];
Delete a file
[FM removeitematpath:@ "filename" error:null];
error: The parameter is a pointer to the nserror object that can provide the wrong information. If NULL is specified, the default behavior is used, and the return value is a method of type bool, and the operation successfully returns YES instead of returning no
Determine if a file has been deleted
if ([FM removeitematpath:@ "filename" error:null]==no) {
NSLog (@ "File deletion failed");
return 1;
}
Nsfilemanager commonly used file methods:
-(nsdata*) Contentsatpath:path reading data from a file
-(BOLL) 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:path1 andpath:path2 Compare the contents of these two files
-(BOOL) fileexistsatpath:path test file exists
-(BOOL) Isreadablefileatpath:path test file exists and can perform read operations
-(BOOL) Iswritablefileatpath:path test whether the file exists and whether it can perform a write operation
-(nsdictionary*) Attributesofitematpath:path Error:err Get the properties of the file
The property dictionary allows you to specify the permissions of the file to be created, and if the parameter is specified as nil, the file is set to the default permissions.
1, through a program to operate the file:
[CPP]View Plaincopyprint?
- //
- //MAIN.M
- //Nsfilemanager_01
- //
- //Created by Swinglife on 13-11-10.
- //Copyright (c) 2013 Swinglife. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- int Main (int argc, const char * argv[])
- {
- @autoreleasepool {
- //File name
- NSString *filename = @"testfile";
- NSString *filecontent = @"This is the file content!!!!" ;
- NSData *filedata = [Filecontent datausingencoding:nsutf8stringencoding];
- //Create Nsfilemanager instances
- Nsfilemanager *FM = [Nsfilemanager Defaultmanager];
- //Create file
- [FM createfileatpath:filename Contents:filedata Attributes:nil];
- //Determine if the file exists or does not exist on the end program
- if ([FM fileexistsatpath:filename]==no) {
- NSLog (@"file does not exist");
- return 1;
- }
- //Copy files
- if ([FM copyitematpath:filename topath:@"NewFile" error:null]==no) {
- NSLog (@"copy failed");
- return 2;
- }
- //test whether two files are the same
- if ([FM contentsequalatpath:filename andpath:@"NewFile"]==no) {
- NSLog (@"file is not the same");
- return 3;
- }
- //Rename NewFile
- [FM moveitematpath:@ "newFile " topath:@"NewFile2" Error:null];
- //Get the size of the NewFile2
- Nsdictionary *FILEATTR = [fm attributesofitematpath:@"NewFile2" Error:null];
- if (fileattr!=nil) {
- NSLog (@"File Size:%llu bytes", [[FileAttr Objectforkey:nsfilesize] unsignedlonglongvalue]);
- }
- //delete files
- [FM removeitematpath:filename Error:null];
- //display content of NewFile2
- NSString *data = [NSString stringwithcontentsoffile:@"NewFile2" encoding:nsutf8stringencoding Error: NULL];
- NSLog (@"%@", data);
- }
- return 0;
- }
Nsfilemanager Common directory Methods
-(nsstring*) Currentdirectorypath get current directory
-(BOOL) Changecurrentdirectorypath:path change the current directory
-(BOOL) copyitematpath:from topath:to error:err Copy directory structure
-(BOOL) Createdirectoryatpath:path withintermediatedirectories: (BOOL) flag attributes:attr Create a new directory
-(BOOL) Fileexistsatpath:path isdirectory: (bool*) flag test file is not a directory (store results in flag)
-(nsarray*) contentsofdirectoryatpath:path Error:err Listing directory contents
-(nsdirectoryenumerator*) Enumeratoratpath:path The contents of the enumeration directory
-(BOOL) Removeitematpath:path error:err Delete Empty directory
-(BOOL) moveitematpath:from topath:to error:err Rename or move a directory
2, through a program to operate the directory:
[CPP]View Plaincopyprint?
- //
- //MAIN.M
- //Nsfilemanager_02
- //
- //Created by Swinglife on 13-11-10.
- //Copyright (c) 2013 Swinglife. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- int Main (int argc, const char * argv[])
- {
- @autoreleasepool {
- //File directory
- NSString *dirname = @"TestDir";
- //Create Nsfilemanager instances
- Nsfilemanager *FM = [Nsfilemanager Defaultmanager];
- //Get current directory
- NSString *PATH = [FM Currentdirectorypath];
- NSLog (@"path:%@", Path);
- //Create a new directory
- [FM createdirectoryatpath:dirname Withintermediatedirectories:yes Attributes:nil Error:null];
- //Rename a new directory
- [FM moveitematpath:dirname topath:@"Newdir" Error:null];
- //Change the current directory to a new directory
- [FM changecurrentdirectorypath:@"Newdir"];
- //Get current working directory
- PATH = [FM Currentdirectorypath];
- NSLog (@"path:%@", Path);
- }
- return 0;
- }