IOS 文件管理,基础使用

来源:互联网 发布:马扎克加工中心编程 编辑:程序博客网 时间:2024/06/07 18:46


#import"ViewController.h"

 

@interfaceViewController ()

 

@end

 

@implementationViewController

 

- (void)viewDidLoad {

    [superviewDidLoad];

   //得到文件管理器

   NSFileManager *fm = [NSFileManagerdefaultManager];

   

//   复制

    [fmcopyItemAtPath:@"/Users/ivan/Desktop/a.png"toPath:@"/Users/ivan/Desktop/copy/a.png"error:nil];

//   移动 移动的时候目的地不能有重名的

    [fmmoveItemAtPath:@"/Users/ivan/Desktop/a.png"toPath:@"/Users/ivan/Desktop/copy/a.png"error:nil];

//   删除

   NSError *err =nil;

    [fmremoveItemAtPath:@"/Users/ivan/Desktop/copy/a.png"error:&err];

   if (err) {

       NSLog(@"出错了");

   }

   

//   创建文件夹

    [fmcreateDirectoryAtPath:@"/Users/ivan/Desktop/a/b/c/d/e/f"withIntermediateDirectories:YESattributes:nilerror:nil];

   

   

//   判断文件是否存在

   if ([fmfileExistsAtPath:@"/Users/ivan/Desktop/lany.txt"]) {

       NSLog(@"文件存在");

   }

   

//   判断文件是否存在是否是文件夹

   BOOL isDir =NO;

   if ([fmfileExistsAtPath:@"/Users/ivan/Desktop/桌面.zip"isDirectory:&isDir]){

       NSLog(@"存在");

       

       if (isDir) {

           NSLog(@"是文件夹");

        }else{

           NSLog(@"不是文件夹");

       }

 

       

       

    }else{

       NSLog(@"不存在");

   }

   

//   获取文件夹里面所有的内容

 

   NSArray *fileNames = [fmcontentsOfDirectoryAtPath:@"/Users/gj/Desktop/"error:nil];

   

   for (NSString *fileNamein fileNames) {

       NSLog(@"%@",fileName);

   }    

}

 

 

@end

0 0
原创粉丝点击