对文件进行 创建 复制 移动 删除等操作


//documents路径
#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject]

//Caches路径
#define kCachePath [ NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) lastObject]
-----------------------------------------------------------------------

- (void)viewDidLoad {
    [superviewDidLoad];
   
// Do any additional setup after loading the view.
   
   
//管理文件的一个类
   
//单例类
   
//可以对文件进行 创建 移动 复制 删除

    [
selfcreateFile];
    [
selfmoveFile];
    [
selfcopyFile];
    [
selfdeleteFile];
    [
selfisExecutable];
}
-----------------------------------------------------------------------
对文件进行 创建 移动 复制 删除
可以分三步来进行:
1.获取文件路径
2.拼接路径
3.创建文件管理器  进行文件的创建移动 复制 删除

//创建文件夹方法
- (
void)createFile
{
   
//获取要创建文件夹的路径
   
NSString *path = [kDocumentPathstringByAppendingPathComponent:@"/DownLoad"];
   
NSLog(@"%@",path);
   
   
//创建文件管理器
   
//withIntermediateDirectories
   
//是否复制之前的文件如果写NO就是不覆盖
   
//不覆盖即创建文件夹失败
   
   
NSFileManager*fileManager = [NSFileManagerdefaultManager];
   
BOOL isCreate = [fileManagercreateDirectoryAtPath:pathwithIntermediateDirectories:YESattributes:nilerror:nil];
   
NSLog(@"%d",isCreate);
   
   
}
-----------------------------------------------------------------------


//移动文件夹
- (
void)moveFile
{
   
//获取原路径
   
NSString *oldPath = [kDocumentPathstringByAppendingPathComponent:@"/DownLoad"
];
   
//获取新路径
   
NSString *newPath = [kCachePathstringByAppendingPathComponent:@"/DownLoad"
];
   
NSLog(@"%@",oldPath);
   
//创建文件管理器管理对象
   
NSFileManager *manager = [NSFileManager  defaultManager
];
   
//移动文件夹(从老的移动到新的路径)
   
BOOL isMove = [manager  moveItemAtPath:oldPath toPath:newPath error:nil];
   
   
NSLog(@"%d",isMove);
}
-----------------------------------------------------------------------


//拷贝文件夹
- (
void)copyFile
{
   
//获取原路径
   
NSString *oldPath = [kCachePathstringByAppendingPathComponent:@"/DownLoad"];
   
//获取新路径
   
NSString *newPath = [kDocumentPathstringByAppendingPathComponent:@"/DownLoad"];
   
NSLog(@"%@",oldPath);
   
NSLog(@"%@",newPath);
   
   
NSFileManager *manager = [NSFileManagerdefaultManager];
   
//拷贝
   
BOOL isCopy = [manager copyItemAtPath:oldPathtoPath:newPatherror:nil];
   
NSLog(@"%d",isCopy);
   
   
}
-----------------------------------------------------------------------


//删除文件夹
- (
void)deleteFile
{
   
//获取要删除的路径
   
NSString *path = [kDocumentPathstringByAppendingPathComponent:@"/DownLoad"];
   
//创建文件管理对象
   
NSFileManager *manager = [NSFileManagerdefaultManager];
   
//删除
   
BOOL isRemove = [manager removeItemAtPath:patherror:nil];
}
-----------------------------------------------------------------------


//判断文件夹是否存在
- (
void)isExecutable
{
   
//获取要判断的路径
   
NSString *path = [kCachePathstringByAppendingPathComponent:@"/DownLoad"];
   
//创建文件管理对象
   
NSFileManager *manager = [NSFileManagerdefaultManager];
   
//判断是否存在
   
BOOL isExecutable =  [manager isExecutableFileAtPath:path];
   
NSLog(@"%d",isExecutable);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章