标签:
创建方式
1 1)直接赋值 2 NSString *str =@"abc"; 3 2)创建对象 4 NSString *str2 = [[NSString alloc]init]; 5 str2 =@"abc" 6 3)格式化创建 7 NSString *str3=[NSString stringWithFromat :@"@",@"abc"]; 8 9 4)从文件中获取 10 NSString strand =[NSString stringWithContentsOfFile:@"/Users/apple/Desktop/test.txt" usedEncoding:NSUTF8StringEncoding error:&err]; 11 12 13 5)把字符串写入到文件 14 NSString *str7 = @"abc"; 15 //路径 //原子性 16 str7 writeToFile :(NNString *) atomically :(BOOL) 17 //编码格式 //错误信息 18 encoding:(NSStringEncoding) error:(NSError **); 19 20 21 NSERROR *err=nil; 22 BOOL flag=str7 writeToFile:@"/Users/apple/Desktop/test.txt" atomically:NO encoding : NSUTF8StringEncoding error:(&err]; 23 if(flag){ 24 NSLog(@"写入成功"); 25 } 26 27 if(err != nil){ 28 NSLog(@"写入失败"); 29 }else{ 30 NSLog(@"写入成功"); 31 }
使用NSURL读写字符串
1 //1)使用NSURL 写入字符串数据到文件中 2 3 NSURL *writeUrl = [NSURL URLWithString:@"file:///Users/apple/Desktop/1.txt"]; 4 5 NSString *str =@"itcast"; 6 7 BOOL isSucess = str writeToURL :writeUrl atomically:NO encoding:NSUTF8StringEncoding error:nil]; 8 9 if(isSucess){ 10 NSLog(@"写入成功"); 11 } 12 13 14 15 //2)使用NSURL读取字符串到文件 16 17 NSURL *readUrl =[NSURL URLWithString:@"file:////Users/apple/Desktop/1.txt"] 18 NSString *straighten = [NSString stringWithContentsOfURL:readUrl encoding :NSUTF8StringEncoding error:nil]; 19 NSLog(@"%@",str2);
标签:
原文地址:http://www.cnblogs.com/developer-wang/p/4507360.html