iOS 文件处理

NSString* imagePath = [[NSBundle mainBundle]pathForResource:@"menu_default" ofType:@"png"];

NSData* imageData = [NSData dataWithContentsOfFile:imagePath];

NSLog(@"imageData-%@",imageData);

//初始化文件管理器

NSFileManager* fileManager = [NSFileManager defaultManager];

//获取文件目录

NSArray* pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString* path = [pathArr objectAtIndex:0];

NSString* path1 =[path stringByAppendingPathComponent:@"txt.txt"];

NSString* path2 =[path stringByAppendingPathComponent:@"test.txt"];

[imageData writeToFile:path1 atomically:YES];

//    [@"123" writeToFile:path2 atomically:YES encoding:NSUTF8StringEncoding error:nil];//写入文件

//移动文件---只能把文件移动到一个空的文件里

//    if ([fileManager moveItemAtPath:path1 toPath:path2 error:nil]) {

//

//        NSLog(@"移动文件");

//    }

//复制文件---只能把文件复制到一个空的文件里

//    if ([fileManager copyItemAtPath:path1 toPath:path2 error:nil]) {

//

//        NSLog(@"复制文件");

//    }

//删除文件

//    if ([fileManager removeItemAtPath:path2 error:nil]) {

//        NSLog(@"删除文件");

//    }

//写入文件

[fileManager createFileAtPath:path2 contents:[@"123" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

//读取文件

NSData* data = [fileManager contentsAtPath:path2];

NSString* str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"str----%@",str);

//    if ([fileManager isReadableFileAtPath:path2]) {

//

//        NSLog(@"是否可读");

//    }

//

//

//    if ([fileManager isWritableFileAtPath:path1]) {

//        NSLog(@"是否可写");

//

//    }

//    if ([fileManager isExecutableFileAtPath:path1]) {

//        NSLog(@"是否可执行");

//    }

//    //比较两个文件的内容

//    if ([fileManager contentsEqualAtPath:path1 andPath:path2]) {

//        NSLog(@"成功");

//    }else{

//        NSLog(@"失败");

//

//    }

//检查文件是否存在

//    if ([fileManager fileExistsAtPath:path]) {

//

//        UIAlertController* alertC = [UIAlertController alertControllerWithTitle:@"系统公告" message:@"目录存在" preferredStyle:UIAlertControllerStyleAlert];

//        UIAlertAction* alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

//

//        }];

//        [alertC addAction:alertAction];

//

//        AppDelegate* delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

//        [delegate.window.rootViewController presentViewController:alertC animated:YES completion:nil];

//    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文链接http://www.cnblogs.com/kenshincui/p/4186022.html 音频在i...
    Hyman0819阅读 21,901评论 4 74
  • 一、iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立、封闭、安全的空间,叫做沙盒。它一...
    1d5cb7cff98d阅读 1,801评论 0 0
  • 一、iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立、封闭、安全的空间,叫做沙盒。它一...
    绚雨蓝了个枫阅读 4,170评论 0 2
  • 一、iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立、封闭、安全的空间,叫做沙盒。它一...
    tzhtodd阅读 1,309评论 0 2
  • iOS开发-文件管理(一) 一、iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立、封闭...
    MacShare阅读 1,821评论 0 6