首页 > 其他 > 详细

NSFileManager的简单介绍,在沙盒目录下对文件进行增删改查

时间:2015-10-29 00:32:32      阅读:401      评论:0      收藏:0      [点我收藏+]

ios应用程序中所产生的所有资源和数据都存放在它的沙盒目录下,沙盒目录中主要包含三个文件夹,

沙盒路径的获取:

NSLog(@"%@",NSHomeDirectory());//打印出相关路径,在前往文件夹下达到

Documents:将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录

//到达Documents文件夹下
  NSArray *storeFile = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *docunmentDirectory = [storeFile objectAtIndex:0];

Library:

          Caches:存放缓存文件,保持数据的持久化

          Preferences:偏好设置

 //到达Library目录下
    NSArray *librarypaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [librarypaths objectAtIndex:0];

 

//到达缓存目录下
    NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [cache objectAtIndex:0];

 tmp:存放零时文件,重启时删除

//到达tmp目录下
    NSString *tmpDirectory = NSTemporaryDirectory();

 

   

NSFileManager的使用:

1.使用NSFileManager创建文件,在沙盒路径下创建一个想要的文件和文件夹
//初始化NSFileManager
    NSFileManager *manager = [NSFileManager defaultManager];
    //创建文件夹相关路径(在document文件夹下创建)
    NSString *filepath = [docunmentDirectory stringByAppendingPathComponent:@"moxue.plist"];
    [manager createFileAtPath:filepath contents:nil attributes:nil];
    //创建moxue文件夹
    NSString *documentpath = [docunmentDirectory stringByAppendingPathComponent:@"moxue"];
     [manager createFileAtPath:documentpath contents:nil attributes:nil];
 //使用NSFileManager创建文件
    //创建文件的属性为attributes,可通过attributesOfItemAtPath中的属性列表查看,对attributes传入相关的字典就可以更改了。
    //通过attributesOfItemAtPath中的属性列表查看
    //    NSLog(@"%@",[manager attributesOfItemAtPath:filepath error:nil]);

2.通过字符串来创建文件,并将字符串写入相应的文件中

//通过字符串来创建文件,将字符串word写入filepath2文件路径中
    NSString *word = @"墨雪";
    NSString *filepath2 = [NSHomeDirectory() stringByAppendingPathComponent:@"moxue1"];
    [word writeToFile:filepath2 atomically:YES encoding:NSUTF8StringEncoding error:nil];

3.在沙盒下移动文件

//移动文件
    NSString *destation = [[NSHomeDirectory() stringByAppendingPathComponent:@"moxue"] stringByAppendingPathComponent:@"moxue2.plist"];
    [manager moveItemAtPath:filepath2 toPath:destation error:nil];

4.沙盒下拷贝文件

//拷贝文件
    NSString *copypath = [[NSHomeDirectory() stringByAppendingPathComponent:@"moxue"]stringByAppendingPathComponent:@"copy.plist"];
    [manager copyItemAtPath:destation toPath:copypath error:nil];

5.在沙盒下删除文件

 //删除文件
    [manager removeItemAtPath:copypath error:nil];

6.在沙盒下查找文件

//查找文件,得到一个数组,得到数组中所有的数据
    NSArray *array = [manager contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"moxue"] error:nil];
    NSLog(@"array = %@",array);

 7.在沙盒下写入数据

    //在文件中写入数据
    NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"dsada122" forKey:@"item12"];
    [dict2 writeToFile:destation atomically:YES];
    NSDictionary *dict22 = [NSDictionary dictionaryWithContentsOfFile:destation];

 

NSFileManager的简单介绍,在沙盒目录下对文件进行增删改查

原文:http://www.cnblogs.com/moxuexiaotong/p/4918962.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!