iOS 文件目录

来源:互联网 发布:淘宝售后多久不能申请 编辑:程序博客网 时间:2024/06/15 14:15

   

    //模拟器下 XXX.APP的目录------添加到项目的东西   这里的东西不能写


    //模拟器的家目录

   NSString *homePath = NSHomeDirectory();

   NSLog(@"%@",homePath);

    //模拟器下 XXX.APP的目录

   NSBundle *bundle = [NSBundlemainBundle];

   NSLog(@"%@",bundle);

   NSString *plistPath = [bundle pathForResource:@"test" ofType:@"plist"];

   NSLog(@"%@",plistPath);

    //文件管理

    NSFileManager *fileMg = [NSFileManagerdefaultManager];

   if([fileMg fileExistsAtPath:plistPath])

    {

       //字典读取

        NSMutableDictionary *dic = [NSMutableDictionarydictionaryWithContentsOfFile:plistPath];

       NSLog(@"%@",dic);

    }

   else

    {

        NSLog(@"file not exist");

    }


    //操作Document   可读写


    NSArray *array =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);//如果为NO则文件写不进去

   NSString *docPath = [array objectAtIndex:0];

   NSLog(@"%@",docPath);

   //构造路劲

   NSString *appendPath = [NSStringstringWithFormat:@"%@/hello.txt",docPath];

   NSLog(@"%@",appendPath);

   NSString *str = [NSStringstringWithFormat:@"hello world"];

    //写入文件

    [str writeToFile:appendPathatomically:YESencoding:NSUTF8StringEncodingerror:nil];