iOS开发沙盒机制文件操作

上一篇写到了沙盒机制的原理,下面来进行实战,详细介绍一下根据沙盒机制来进行文件的各种操作

1.写入

//获取Documents文件路径

    NSString *doucumentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];


#pragma mark - 字符串写入本地

    //拼接文件路径 下面两种方法均可

         //拼接的路径test.TXT自己随便给,不过后缀应该注意一下

    NSString *filePath = [doucumentsPath stringByAppendingPathComponent:@"test.TXT"];

    //NSString *filePath = [doucumentsPath stringByAppendingString:@"/test.TXT"];

    //写入

    [str1 writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

#pragma mark - 数组写入本地

    //数组

    NSArray *arr = @[str1,str2];

    //拼接文件路径

    NSString *filePath = [doucumentsPath stringByAppendingPathComponent:@"arr.plist"];

    //写入

    [arr writeToFile:filePath atomically:YES];


#pragma mark - 字典的写入本地

    //字典

    NSDictionary *dic = @{@"tf1":_Tf1.text,@"tf2":_Tf2.text};

    //文件拼接

    NSString *filePath = [doucumentsPath stringByAppendingPathComponent:@"dic.plist"];

    //写入

    [dic writeToFile:filePath atomically:YES];

#pragma mark - NSData的写入本地

    //data

    NSData *data = [str1 dataUsingEncoding:NSUTF8StringEncoding];

    //文件拼接

    NSString *filePath = [doucumentsPath stringByAppendingPathComponent:@"data.TXT"];

    //写入

    [data writeToFile:filePath atomically:YES];


2.读取

    //获取文件路径

    //获取documnets文件路径

    NSString *documnetsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];


#pragma mark - 字符串读取

    //拼接文件路径

    NSString *filePath = [documnetsPath stringByAppendingPathComponent:@"test.TXT"];

    //读取

    NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

#pragma mark - 数组读取

   //文件路径拼接

    NSString *filePath = [documnetsPath stringByAppendingPathComponent:@"arr.xml"];

    //读取

    NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];

#pragma mark - 字典读取

    //文件路径拼接

    NSString *filePath = [documnetsPath stringByAppendingPathComponent:@"dic.plist"];

    //读取

    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];

#pragma  mark - data读取

    //文件路径拼接

    NSString *filePath = [documnetsPath stringByAppendingPathComponent:@"data.TXT"];

    //读取

    NSData *data = [NSData dataWithContentsOfFile:filePath];

   当然NSData数据要进行转化之后进行数据的操作


3.创建

 //获取documents 文件路径

    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];

    //拼接文件夹路径

    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Images"];

    //创建文件管理对象

    NSFileManager *manager = [NSFileManager defaultManager]; 

    //创建

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


4.移动

 //Images移动到 Caches

    //oldPath

    //获取tmp文件路径

    NSString *tempPath = NSTemporaryDirectory();

    //拼接

    NSString *oldPath = [tempPath stringByAppendingPathComponent:@"Images"];

    

    //newPath

    //获取Caches文件路径

    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];

    //拼接

    NSString *newPath = [cachesPath stringByAppendingPathComponent:@"Images"];

    //创建文件管理对象

    NSFileManager *manager = [NSFileManager defaultManager];

    

    //移动

    [manager moveItemAtPath:oldPath toPath:newPath error:nil];


5.复制

    //1.获取tmp文件路径

    NSString *tmp = NSTemporaryDirectory();

    //2.文件拼接

    NSString *newPath = [tmp stringByAppendingPathComponent:@"Images"];

    //创建文件管理对象

    NSFileManager *manager = [NSFileManager defaultManager];

    

    //获取doucuments 文件路径

    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

    //文件拼接

    NSString *oldPath = [documentsPath stringByAppendingPathComponent:@"Images"];

    

    //复制

    [manager copyItemAtPath:oldPath toPath:newPath error:nil];


6.删除(清除缓存)

 //获取Caches文件路径

    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];

    //拼接

    NSString *filePath = [cachesPath stringByAppendingPathComponent:@"Images"];

    

    //创建文件管理对象

    NSFileManager *manager = [NSFileManager defaultManager];

    

    [manager removeItemAtPath:filePath error:nil];


7.文件是否存在

//获取caches文件路径

    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];

    //拼接

    NSString *filePath = [cachesPath stringByAppendingPathComponent:@"Images"];

    

    //创建文件管理对象

    NSFileManager *manager = [NSFileManager defaultManager];

    

    //判断是否存在

    BOOL isExist = [manager fileExistsAtPath:filePath];


希望对大家有所帮助


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值