《 iOS文件夹与plist文件的创建与删除》

2018-09-25  本文已影响0人  北漂老张

 《 iOS文件夹与plist文件的创建与删除》

//  ViewController.m 

//  fileManagerExe 

//  Created by a111 on 16/4/8. 

#import "ViewController.h" 

@interface ViewController () 

@end 

@implementation ViewController 

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    //找到相应的目录 

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

    NSString *path = [paths objectAtIndex:0]; 

   //目录下的文件夹名字'aaa' 

    NSString *pathFile = [path stringByAppendingPathComponent:@"aaa"]; 

    //判断有没有文件夹 

    BOOL isDir =NO; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    BOOL existed = [fileManager fileExistsAtPath:pathFile isDirectory:&isDir]; 

    if ( !(isDir ==YES && existed == YES) ){ 

        //如果没有文件夹则创建 

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

    }else{ 

        //删除文件夹 

        //[fileManager removeItemAtPath:pathFile error:nil]; 

    } 

    //在目录文件夹下找相应文件'bbb.plist' 

    NSString *filename=[pathFile stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",@"bbb"]]; 

    if (![fileManager fileExistsAtPath:filename]) { 

        //如果没有文件,添加新内容,生成新文件 

        NSDictionary *dic = @{@"aaa":@"1"};//(内容随便写的) 

        [dic writeToFile:filename atomically:YES]; 

    }else{ 

        //如果有文件,获取文件夹文件 

        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filename]; 

        NSLog(@"%@",dic); 

    } 

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

- (void)didReceiveMemoryWarning { 

    [super didReceiveMemoryWarning]; 

    // Dispose of any resources that can be recreated. 

@end 

上一篇下一篇

猜你喜欢

热点阅读