创建文件 大合集

   
    //查找文件路径的函数
   NSString *path= [[NSBundle mainBundle]pathForResource:@"student" ofType:@"txt"];
    NSError *error=nil;
    //stringWithContentsOfFile 是读取 指定路径里的内容    NSUTF8StringEncoding:是对文本编码的
    NSString * str1=[NSString stringWithContentsOfFile:path 
                                             encoding:NSUTF8StringEncoding 
                                                error:&error];
    if (error)
    {
        NSLog(@"oops errpr:%@",[error localizedDescription]);
    }
    NSLog(@"str1= %@",str1);
    
    
    
  */
    //
    NSString *str1=@"abc";
    //在指定路径创建文件及 并在文件里面写东西
    NSString *path2=@"/Users/ibokan/Desktop/k.txt";
    NSError *error2=nil;
    NSString *str2=[str1 stringByAppendingFormat:@"%@",@"12"];
    NSLog(@"%@",str2);
   BOOL success= [str2 writeToFile:path2
           atomically:YES   //YES NO yes的话 先放到缓存里面  no的话 直接往里面放
             encoding:NSUTF8StringEncoding 
                error:&error2];
    
    if (!success)
    {
        NSLog(@"error2=%@",[error2 localizedFailureReason]);
    }
    
    
    /*
    
    //NSData  二进制        initWithContensOfFile  需要释放
    NSData *stuData=[NSData dataWithContentsOfFile:path];
    NSString *stuStr=[[NSString alloc]initWithData:stuData 
                                          encoding:NSUTF8StringEncoding];
    NSLog(@"stustr=%@",stuStr);
    
    
    //NSFileManger
    //创建目录
    NSFileManager *manager=[NSFileManager defaultManager];
    NSString *path3=@"/Users/ibokan/Desktop/abc";
    NSString *path4=@"/Users/ibokan/Desktop/1/1abc.txt";
    //创建目录 就是文件夹     withIntermediateDirectories:中间目录   attributes:属性 
BOOL succes2= [manager createDirectoryAtPath:path3
       withIntermediateDirectories:YES
                        attributes:nil
                             error:nil];
    
    NSLog(@"succes2:=%d",succes2);
                
    //创建文件        string 和NSData 能相互互转
    NSData *stuFileData=[@"llllxxxxdddd" dataUsingEncoding:NSUTF8StringEncoding];
    BOOL s3=[manager createFileAtPath:path4 contents:stuFileData attributes:nil];
    NSLog(@"s3=%d",s3);
    
    //删除文件
    BOOL s4=[manager removeItemAtPath:path4 error:nil];
    NSLog(@"s4=%d",s4);
    //移动 mov
    
    //
 */
    /*
    NSString *path5= [[NSBundle mainBundle]pathForResource:@"baga" ofType:@"plist"];
    NSDictionary *dic=[NSDictionary dictionaryWithContentsOfFile:path5];
//     NSArray *array=[NSArray arrayWithContentsOfFile:path5];
    
    
    
    NSArray *array=[dic allKeys];
    
    NSLog(@"array  =%@",array);
    NSLog(@"a%@",[dic objectForKey:@"New item"] );
    
    NSLog(@"dic=%@",dic);
    //XML
   



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值