ios 文件操作

/*****************************************

 每个ios程序都有一个这样的目录

 Documents

 Library

 tmp

 ****************************************/

/***找到app的沙箱目录**/

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

    NSString *documentsDirectory = [path objectAtIndex:0]; //path数组里貌似只有一个元素

    NSLog(@"path元素个数:%d",[path count]);

    NSLog(@"%@",documentsDirectory);

    appPath.text = documentsDirectory;

    /***新建一个文件****/

    NSString *fname = [documentsDirectory stringByAppendingPathComponent:@"newfile.txt"];

    NSLog(@"新建文本文件路径是:%@",fname);

    //写入文字到新建文本文件

    NSMutableArray *array = [[NSMutableArrayalloc]init];

    [array addObject:@"aaa"];

    [array addObject:@"bbb"];

    [array addObject:@"ccc"];

    [array writeToFile:fname atomically:YES];

    //读取新建的文本文件的内容

    if([[NSFileManagerdefaultManager] fileExistsAtPath:fname])

    {

        NSArray *array = [[NSArray alloc] initWithContentsOfFile:fname];

        NSLog(@"%@",[array objectAtIndex:0]);

    }

 

- (IBAction)GetAbsoluteUrl {
    //相对,绝对路径问题
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"%@",documentsDirectory);//documents目录路径
    //Users/jstv/Library/Application Support/iPhone Simulator/5.1/Applications/12C32E44-1C6F-4DA1-AC98-DF9C9247CBC5/Documents
    NSLog(@"%@",NSHomeDirectory());//app文件所在目录路径
    //Users/jstv/Library/Application Support/iPhone Simulator/5.1/Applications/12C32E44-1C6F-4DA1-AC98-DF9C9247CBC5
    NSLog(@"临时文件夹%@",NSTemporaryDirectory()); //tmp目录路径
    ///var/folders/wq/15313ptj2xl_b3zdwjkvd6wc0000gn/T/
    NSLog(@"%@",[[NSBundle mainBundle] resourcePath]);//程序应用包路径
    //Users/jstv/Library/Application Support/iPhone Simulator/5.1/Applications/12C32E44-1C6F-4DA1-AC98-DF9C9247CBC5/fileManage.app
    //或者
    NSLog(@"aaa%@",[[NSBundle mainBundle] pathForResource: @"newfile" ofType: @"txt"]);
    //Users/jstv/Library/Application Support/iPhone Simulator/5.1/Applications/12C32E44-1C6F-4DA1-AC98-DF9C9247CBC5/fileManage.app/bomb.jpg
}
posted on 2012-06-06 10:22  人的本质是什么? 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/PirateCaptain/articles/2537783.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值