首页 > 移动平台 > 详细

iOS NSFileManager文件操作

时间:2014-07-26 02:09:16      阅读:333      评论:0      收藏:0      [点我收藏+]

参考:http://blog.csdn.net/xyz_lmn/article/details/8968213 感谢这些先行者

文件操作常用操作记录一下:

 //获取沙盒的根路径
    NSString *dirHome = NSHomeDirectory();
    NSLog(@"%@",dirHome);
    //获取Documents目录路径
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [path objectAtIndex:0];
    NSLog(@"%@",documentsDirectory);
    //获取Library 目录路径
    path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory  = [path objectAtIndex:0];
    NSLog(@"%@",libraryDirectory);
    //获取caches目录路径
    path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *caches = [path objectAtIndex:0];
    NSLog(@"%@",caches);
    //获取Tmp 目录路径
    NSString *tmpDirectory = NSTemporaryDirectory();
    NSLog(@"%@",tmpDirectory);
    //创建文件夹
    //1 获得文件路径
    //2 创建文件管理器
    //3 用文件管理器在目录上创建文件
    path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES);
    documentsDirectory = [path objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
    NSFileManager *fm = [NSFileManager defaultManager];
    BOOL res = [fm createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    if(!res)
    {
        NSLog(@"文件夹创建成功");
    }
    else
    {
        NSLog(@"文件夹创建失败");
    }
    //创建文件(与文件夹的创建相同)
    path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    documentsDirectory = [path objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"text"];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"123.txt"];
    fm = [NSFileManager defaultManager];
    res = [fm createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    if(res)
    {
        NSLog(@"文件夹创建成功");
    }
    else
    {
        NSLog(@"文件夹创建失败");
    }
    //写数据到文件
    path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    documentsDirectory = [path objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"text"];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"123.txt"];
    NSString *content = @"文件写入的内容";
    res = [content writeToFile:documentsDirectory atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if(res)
    {
        NSLog(@"文件夹创建成功");
    }
    else
    {
        NSLog(@"文件夹创建失败");
    }
    //读文件数据
    path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    documentsDirectory = [path objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"text"];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"123.txt"];
    content  = [NSString stringWithContentsOfFile:documentsDirectory encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"文件读取成功:%@",content);
    //文件的属性
    path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    documentsDirectory = [path objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"text"];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"123.txt"];
    fm = [NSFileManager defaultManager];
    NSDictionary *fileAttributes = [fm attributesOfItemAtPath:documentsDirectory error:nil];
    NSArray *keys;
    id key,value;
    keys = [fileAttributes allKeys];
    int count = [keys count];
    for (int i= 0; i<count; i++)
    {
        key = [keys objectAtIndex:i];
        value = [fileAttributes objectForKey:key];
        NSLog(@"Key:%@ for value :%@",key,value);
    }
    //删除文件
    path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    documentsDirectory = [path objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"text"];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"123.txt"];
    fm = [NSFileManager defaultManager];
    res=[fm removeItemAtPath:documentsDirectory error:nil];
    if(res)
    {
        NSLog(@"文件删除成功");
    }
    {
        NSLog(@"文件删除失败");
    }





iOS NSFileManager文件操作,布布扣,bubuko.com

iOS NSFileManager文件操作

原文:http://blog.csdn.net/u010123208/article/details/38128525

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