子曾经曰过

  博客园  :: 首页  ::  ::  ::  :: 管理

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

 每个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  人的本质是什么?  阅读(233)  评论(0编辑  收藏  举报