iOS开发之Objective-C读写文件

以name,phoneNumber,age三个字段,为例。在iphone上,在文本框中输入数据,获取后,装入数组中。然后把数组写入.plist文件

写操作

    NSString *name=[txt1 text];
    NSNumber *phoneNumber=[[NSNumber alloc] initWithInt:[[txt2 text] intValue]];
    NSNumber *age=[[NSNumber alloc] initWithInt:[[txt3 text] intValue]];
    
    NSMutableArray *array=[[NSMutableArray alloc]init];
    [array  addObject:name];
    [array  addObject:phoneNumber];
    [array  addObject:age];

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:@"personal.plist"];    
    
    [array writeToFile:filename  atomically:YES];
    [array release];    

读操作

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:@"personal.plist"];
    
    NSMutableArray *array=[[NSMutableArray alloc] initWithContentsOfFile:filename];
    
    txt1.text=[array objectAtIndex:0];
    txt2.text=[[NSString alloc] initWithFormat:@"%d",[[array objectAtIndex:1]intValue]];
    txt3.text=[[NSString alloc] initWithFormat:@"%d",[[array objectAtIndex:2]intValue]];

    [array release];

读写文件

NSString *path = @"/Users/Allen/Desktop/test.dat";
 
NSString *temp = @"Allen Dang";
int i = 100;
float f = 98.3333f;

NSMutableData *writer = [[NSMutableData alloc] init];
 
[writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
[writer appendBytes:&i length:sizeof(i)];
[writer appendBytes:&f length:sizeof(f)];
 
[writer writeToFile:path atomically:YES];
[writer release];

读文件

NSString *contentPath = [[NSBundle mainBundle] pathForResource:@"Content" ofType:@"txt"];
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *txtContent = [NSString stringWithContentsOfFile:contentPath encoding:NSUTF8StringEncoding error:nil];

写文件

NSString *str = @"asdasdads";
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSData *data = [str dataUsingEncoding:enc];
NSString *filename = @"/Users/xxx/files.txt"];
[data writeToFile: filename atomically: NO];
int ii;
float ff;
NSString *ttemp;
 
NSData *reader = [NSData dataWithContentsOfFile:path];
ttemp = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])]
                                  encoding:NSUTF8StringEncoding];
[reader getBytes:&ii range:NSMakeRange([temp length], sizeof(ii))];
[reader getBytes:&ff range:NSMakeRange([temp length] + sizeof(ii), sizeof(ff))];
 
NSLog(@"string:%@  int:%i float:%f", ttemp, ii, ff);
 
[temp release];
[ttemp release];
[path release];

NSString为UTF8编码的函数

@interface NSString (OAURLEncodingAdditions)

- (NSString *)URLEncodedString;

- (NSString *)URLDecodedString;

@end


@implementation NSString (OAURLEncodingAdditions)
- (NSString *)URLEncodedString{    
    NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8);
   [result autorelease];
   return result;
}
- (NSString*)URLDecodedString{
   NSString *result = (NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8);
   [result autorelease];
   return result;
}
@end

你可能感兴趣的:(iOS开发之Objective-C读写文件)