IOS沙盒机制和文件操作(2)

2013年12月10日学习日志:IOS沙盒机制和文件操作(2)

1.    在document里创建目录

//获取当前程序的document的路径

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

    NSString *documentDirectory = [paths objectAtIndex:0];

    NSLog(@"当前程序的document目录%@",documentDirectory);

    //创建文件管理器

    NSFileManager *fileManager = [NSFileManager defaultManager];

    //myTest的路径

    NSString *myTestDirectory = [documentDirectory stringByAppendingPathComponent:@"myTest"];

    //创建目录

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

输出:2013-12-15 19:49:57.294 a[1074:c07] 当前程序的document目录/Users/haixia/Library/ApplicationSupport/iPhoneSimulator/6.1/Applications/B89BC296-EB87-4789-A497-914C3DF8FB55/Documents

 

   

2.    在刚刚创建的目录下创建文件

  //myTest目录下创建文件

    //文件的路径

    NSString*testPsth1 = [myTestDirectory stringByAppendingPathComponent:@"tsst1.txt"];

    NSString*testPath2 = [myTestDirectory stringByAppendingPathComponent:@"test2.txt"];

    NSString*testPath3 = [myTestDirectory stringByAppendingPathComponent:@"test3.txt"];

    //写入的内容

    NSString *string= @"hello,1234567";

    //创建文件

    [fileManager createFileAtPath:testPsth1contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

    [fileManager createFileAtPath:testPath2contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

    [fileManager createFileAtPath:testPath3contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

3.    获取目录列表里的所有文件

 //获取目录列表里的所有文件

    //NSArray *file = [fileManagersubpathsAtPath:myTestDirectory];

    NSArray *file = [fileManager subpathsOfDirectoryAtPath:myTestDirectory error:nil];

    NSLog(@"%@",file);

    NSArray *files = [fileManager subpathsAtPath:myTestDirectory];

    NSLog(@"%@",files);

//   2013-12-15 20:16:31.041 a[1208:c07] 当前程序的document目录/Users/haixia/Library/ApplicationSupport/iPhoneSimulator/6.1/Applications/B89BC296-EB87-4789-A497-914C3DF8FB55/Documents

//   2013-12-15 20:16:31.046 a[1208:c07] (

//                                        ".DS_Store",

//                                        "test2.txt",

//                                        "test3.txt",

//                                        "tsst1.txt"

//                                         )

//   2013-12-15 20:16:31.047 a[1208:c07] (

//                                        ".DS_Store",

//                                        "test2.txt",

//                                        "test3.txt",

//                                         "tsst1.txt"

//                                         )

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值