//Initialize management class
Nsfilemanager * manager = [nsfilemanagerdefaultmanager];
// Path
nsstring * directorypath = [nshomedirectory()stringbyappendingpathcomponent : @ "/desktop/ My folder 1/ my folder 2" ];
nserror * error = nil;
if([ManagerCreatedirectoryatpath:D Irectorypathwithintermediatedirectories:NOattributes:NilError: &error]! =YES) {
NSString * str = [ERROR localizeddescription];
NSLog (@ " Create failed ");
}Else {
NSString * str = [ERROR localizeddescription];
NSLog (@ " Create success ");
}
If this is no, then the middle: My folder 1 (whether or not it existed before) is not created and does not exist before.
Run results
2015-04-11 11:30:41.477 2. Create a folder [852:537431] creation failed
Program ended with exit code:0
If yes, in the middle: My folder 1 (no matter if it was not present) is created, there is no time between
Run results (Create a folder for my folders 1 below him in my folder 2)
2015-04-11 11:38:11.547 2. Create a folder [877:577493] created successfully
Program ended with exit code:0
Two ways to rename a file:
Method One, the idea: 1. Extract the original file data,
2. Save the data to the new file,
3. Delete the original file
method Two, with [manager moveitematpath: FilePathtopath: NewPath Error :nil]; method will filepath
File name under path change to NewPath
Assign a value to the file NSData, after the content changes within the file, the file content after move remains consistent with the last modification
If the file NSData is not assigned, it is changed within the out-of-program file, and the new file remains empty after the move.
Comprehensive Exercises:
Create a new folder, move the files outside the folder to the inside of the folder, and copy a copy
1. How to create a new OC folder
2. Movement of files
3. Copying of files
#import <Foundation/Foundation.h>
int Main (int argc,const Char * argv[]) {
@autoreleasepool {
//File Manager
Nsfilemanager * manager = [nsfilemanagerdefaultmanager];
//Folder address
nsstring * Directorpath = [nshomedirectory()stringbyappendingpathcomponent :@ "Desktop/goods"];
//File address
nsstring * FilePath = [nshomedirectory()STRINGBYAPPENDINGPATHCOMPONENT : @ "Desktop/east.txt" ];
nsstring * filePath2 = [nshomedirectory()stringbyappendingpathcomponent: /c4>@ "Desktop/goods/east.txt"];
//File Data
nsstring * strfordata = @ "Goods is selling good!";
nsdata * dataforfile = [Strfordatadatausingencoding:nsutf8stringencoding];
if ([managerCreatedirectoryatpath:d irectorpath withintermediatedirectories< c4>:YESattributes:nilerror:nil]) {
NSLog (@ "Create success");
}
if ([Manager Fileexistsatpath: FilePath]) {
NSLog (@ "file exists");
}Else{
[Manager Createfileatpath: FilePathcontents:d ataforfile attributes:nil];
NSLog (@ "file created successfully! ");
}
if ([Manager Moveitematpath: FilePath topath: filePath2error:nil]) {
NSLog (@ "mobile success");
}Else{
NSLog (@ "move failed");
}
nsstring * filePath3 = [nshomedirectory()stringbyappendingpathcomponent: /c4>@ "Desktop/goods/1east.txt"];
//Copy
if ([Manager Copyitematpath: filePath2 topath: FilePath3error:nil]) {
NSLog (@ "Success");
}Else{
NSLog (@ "fail");
}
}
return 0;
}
OC Nsfilemanager folder creation, file move, file copy, file Rename