Object-c NSdata 读取和写入二进制文件

发布时间:2014-10-23 23:25:14
来源:分享查询网

//转:http://blog.csdn.net/carmazhao/article/details/8205069 -(void)test_file{ // 获取程序Documents目录路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSMutableString * path = [[NSMutableString alloc]initWithString:documentsDirectory]; [path appendString:@"/fuck"]; NSString *temp = @"我ABC"; NSInteger iLength = [[temp dataUsingEncoding:NSUTF8StringEncoding] length]; NSMutableData * data = [[NSMutableData alloc]init]; [data appendBytes:&iLength length:sizeof(iLength)]; [data appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]]; [data writeToFile:path atomically:YES]; NSInteger fuck = 0; NSData * reader = [NSData dataWithContentsOfFile:path]; [reader getBytes:&fuck length:sizeof(fuck)]; NSString *fff = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(sizeof(fuck) , fuck)] encoding:NSUTF8StringEncoding]; NSLog(@"iLength=%ld, data=%@" , iLength , fff); [fff release]; [path release]; [data release]; }

返回顶部
查看电脑版