关闭

iOS开发沙盒机制文件操作

标签: iosios开发沙盒
72人阅读 评论(0) 收藏 举报
分类:

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

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];


希望对大家有所帮助


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:389次
    • 积分:33
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档