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


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

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

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

    [
self createFile ];
    [
self moveFile ];
    [
self copyFile ];
    [
self deleteFile ];
    [
self isExecutable ];
}
-----------------------------------------------------------------------
对文件进行 创建 移动 复制 删除
可以分三步来进行:
1.获取文件路径
2.拼接路径
3.创建文件管理器  进行文件的创建移动 复制 删除

// 创建文件夹方法
- (
void )createFile
{
   
// 获取要创建文件夹的路径
   
NSString *path = [kDocumentPathstringByAppendingPathComponent:@"/DownLoad" ];
   
NSLog ( @"%@" ,path);
   
   
// 创建文件管理器
   
//withIntermediateDirectories
   
// 是否复制之前的文件 如果写 NO 就是不覆盖
   
// 不覆盖即创建文件夹失败
   
   
NSFileManager*fileManager = [NSFileManagerdefaultManager ];
   
BOOL isCreate = [fileManager createDirectoryAtPath: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 = [ kCachePath stringByAppendingPathComponent : @"/DownLoad" ];
   
// 获取新路径
   
NSString *newPath = [ kDocumentPath stringByAppendingPathComponent : @"/DownLoad" ];
   
NSLog ( @"%@" ,oldPath);
   
NSLog ( @"%@" ,newPath);
   
   
NSFileManager *manager = [ NSFileManager defaultManager ];
   
// 拷贝
   
BOOL isCopy = [manager copyItemAtPath :oldPath toPath :newPath error : nil ];
   
NSLog ( @"%d" ,isCopy);
   
   
}
-----------------------------------------------------------------------


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


// 判断文件夹是否存在
- (
void )isExecutable
{
   
// 获取要判断的路径
   
NSString *path = [ kCachePath stringByAppendingPathComponent : @"/DownLoad" ];
   
// 创建文件管理对象
   
NSFileManager *manager = [ NSFileManager defaultManager ];
   
// 判断是否存在
   
BOOL isExecutable =  [manager isExecutableFileAtPath :path];
   
NSLog ( @"%d" ,isExecutable);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值