iOS 之文件管理器

//在.m文件里

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

#pragma mark --NSFileManager 文件管理器

#pragma mark 1.创建文件夹

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

    NSString *path = [array lastObject];

    NSString *filePath = [path stringByAppendingPathComponent:@"zhaoYing"];

    

    //1.创建文件管理器对象

    NSFileManager *fileManager = [NSFileManager defaultManager];


#warning 重点 2、创建文件夹

    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];

    NSLog(@"filePath = %@", filePath);

    

    

    NSString *Str = @"大好人";

    NSString *filePathZhaoY = [filePath stringByAppendingPathComponent:@"zhaoYing.txt"];

    

#warning 重点 写入本地 writeToFile

    [Str writeToFile:filePathZhaoY atomically:YES encoding:NSUTF8StringEncoding error:nil];

    

    //再创建一个文件夹

    NSString *filePathJCZ = [path stringByAppendingPathComponent:@"JiChangZhi"];


    [fileManager createDirectoryAtPath:filePathJCZ withIntermediateDirectories:YES attributes:nil error:nil];

    

    //将文件夹下的zhaoying.txt移动到JiChangZhi文件夹里

    NSString *filePathJCZT = [filePathJCZ stringByAppendingPathComponent:@"zhaoYing1.txt"];

  /*

  BOOL result = [fileManager moveItemAtPath:filePathZhaoY toPath:filePathJCZT error:nil];

    

    if (result) {

        NSLog(@"移动成功");

    }else

    {

        NSLog(@"移动失败");

    }

    */

    

    //拷贝文件到另一个目录中

   BOOL copyResult =  [fileManager copyItemAtPath:filePathZhaoY toPath:filePathJCZT error:nil];

    if (copyResult) {

        

        NSLog(@"复制成功");

    }else

    {

        NSLog(@"复制失败");


    }

    

    

    

#warning 重点 删除文件

    BOOL removeResult = [fileManager removeItemAtPath:filePathZhaoY error:nil];

    if (removeResult) {

        NSLog(@"删除成功");

    }else

    {

        NSLog(@"删除失败");

    }

   


#warning 重点  判断文件是否存在

    BOOL existsReuslt = [fileManager fileExistsAtPath:filePathJCZT];

    if (existsReuslt) {

        NSLog(@"文件存在");

    }else

    {

        NSLog(@"文件不存在");

    }

    

    

    

    

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值