//转: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];}