iOS学习笔记11-沙盒

  //  取沙盒路径

    NSString *home = NSHomeDirectory();

    NSLog(@"%@",home);

    

    /**

     *文件夹路径

     **/

    

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

    NSLog(@"%lu",arr.count);

    NSString *document = arr[0];

    NSLog(@"%@",document);

    

//    /**

//     *缓存路径

//     **/

//    NSArray *cachespath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

//    NSLog(@"%lu",cachespath.count);

//    

//    NSString *caches = cachespath[0];

//    NSLog(@"%@",caches);

//    

//    

//    /**

//     *临时文件路径

//     **/

//    

//    NSString *temPath = NSTemporaryDirectory();

//    NSLog(@"%@",temPath);

//    // Do any additional setup after loading the view, typically from a nib.

 

    /**

     *文件操作

     **/

    

    //添加文件夹

    NSFileManager *fileManager = [NSFileManager defaultManager];

#warning 加入 / 和不加 / 差别很大

    NSString *newFilePath = [document stringByAppendingString:@"/test"];//拼接新文件夹的路径//创建了一个新的文件夹

    BOOL res = [fileManager createDirectoryAtPath:newFilePath withIntermediateDirectories:YES attributes:nil error:nil];

    if (res) {

        NSLog(@"创建文件夹成功");

    }else{

        NSLog(@"创建文件夹失败");

    }

    //添加文件

    NSString *file = [newFilePath stringByAppendingString:@"/1.txt"];

    BOOL r = [fileManager createFileAtPath:file contents:nil attributes:nil];

    if (r) {

        NSLog(@"添加文件成功");

        

    }else{

        NSLog(@"添加文件失败");

    }

 

 

/**

     *读文件

     **/

    NSString *str = [NSString stringWithContentsOfFile:dataToFile encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"%@",str);

 

    _label.frame = CGRectMake(100, 100, 200, 200);

    _label.backgroundColor = [UIColor redColor];

    _label.font = [UIFont fontWithName:@"Arial" size:20];

    _label.text = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];

    _label.textAlignment = NSTextAlignmentCenter;

 

 

    

posted @ 2016-02-22 10:17  small-elephant_A-Do  阅读(147)  评论(0编辑  收藏  举报