1,写文件操作
1 - (IBAction)btnWrite:(id)sender { 2 //创建文件管理器 3 NSFileManager *fileManager = [NSFileManager defaultManager]; 4 //获取路径 5 //参数 要获取的哪种路径 6 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 7 NSString *documentDirectory = [paths objectAtIndex:0];//去除需要的路径 8 //更改到操作的目录下 9 [fileManager changeCurrentDirectoryPath:[documentDirectory stringByExpandingTildeInPath]]; 10 //创建文件fileName文件名称,contents文件内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil 11 [fileManager createFileAtPath:@"fileName" contents:nil attributes:nil]; 12 //删除待删除文件 13 [fileManager removeItemAtPath:@"createNewFile" error:nil]; 14 //获取文件路径 15 NSString *path = [documentDirectory stringByAppendingPathComponent:@"fileName"]; 16 NSLog(@"path == %@",path); 17 //待写入的数据 18 NSString *temp = @"Hello world"; 19 int data0 = 1000000; 20 float data2 = 23.23f; 21 //创建数据缓冲 22 NSMutableData *writer = [[NSMutableData alloc]init]; 23 //将字符串添加到缓冲中 24 [writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]]; 25 [writer appendBytes:&data0 length:sizeof(data0)]; 26 [writer appendBytes:&data2 length:sizeof(data2)]; 27 28 [writer writeToFile:path atomically:YES]; 29 [writer release]; 30 }
2,读文件操作
1 - (IBAction)btnRead:(id)sender { 2 //创建文件管理器 3 NSFileManager *fileManager = [NSFileManager defaultManager]; 4 //获取路径 5 //参数 要获取的哪种路径 6 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 7 NSString *documentDirectory = [paths objectAtIndex:0];//去除需要的路径 8 //更改到操作的目录下 9 [fileManager changeCurrentDirectoryPath:[documentDirectory stringByExpandingTildeInPath]]; 10 //创建文件fileName文件名称,contents文件内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil 11 [fileManager createFileAtPath:@"fileName" contents:nil attributes:nil]; 12 //删除待删除文件 13 [fileManager removeItemAtPath:@"createNewFile" error:nil]; 14 //获取文件路径 15 NSString *path = [documentDirectory stringByAppendingPathComponent:@"fileName"]; 16 NSLog(@"path == %@",path); 17 //待写入的数据 18 NSString *temp = @"Hello world"; 19 int data0 = 1000000; 20 float data2 = 23.23f; 21 //创建数据缓冲 22 NSMutableData *writer = [[NSMutableData alloc]init]; 23 //将字符串添加到缓冲中 24 [writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]]; 25 [writer appendBytes:&data0 length:sizeof(data0)]; 26 [writer appendBytes:&data2 length:sizeof(data2)]; 27 28 [writer writeToFile:path atomically:YES]; 29 [writer release]; 30 31 //读取操作 32 int gData0; 33 float gData1; 34 NSString *gData2; 35 36 NSData *reader = [NSData dataWithContentsOfFile:path]; 37 gData2 = [[NSString alloc]initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])] encoding:NSUTF8StringEncoding]; 38 [reader getBytes:&gData0 range:NSMakeRange([temp length], sizeof(gData0))]; 39 [reader getBytes:&gData1 range:NSMakeRange([temp length]+ sizeof(gData0) , sizeof(gData1))]; 40 NSLog(@"gData0 == %d",gData0); 41 NSLog(@"gData1 == %f",gData1); 42 NSLog(@"gData2 == %@",gData2); 43 44 // lblText.text = gData2; 45 46 }
iphone实现文件的读写操作
iphone可以很容易的实现文件的读写,但是如果你没有使用苹果公司99美元开发者sdk的话,你利用破解打出来的api包,安装到手机上不能把文件写入手机.下面是我实现读写的代码:
#import "ManagerFile.h"
@implementation ManagerFile
-(void)writeFile:(NSString *)file
{
//创建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//获取路径
//参数NSDocumentDirectory要获取那种路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];//去处需要的路径
//更改到待操作的目录下
[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
//创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
//获取文件路径
[fileManager removeItemAtPath:@"username" error:nil];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"username"];
//创建数据缓冲
NSMutableData *writer = [[NSMutableData alloc] init];
//将字符串添加到缓冲中
[writer appendData:[file dataUsingEncoding:NSUTF8StringEncoding]];
//将其他数据添加到缓冲中
//将缓冲的数据写入到文件中
[writer writeToFile:path atomically:YES];
[writer release];
}
-(NSString *)readFile
{
//创建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//获取路径
//参数NSDocumentDirectory要获取那种路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];//去处需要的路径
//更改到待操作的目录下
[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
//获取文件路径
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"username"];
NSData *reader = [NSData dataWithContentsOfFile:path];
return [[NSString alloc] initWithData:reader
encoding:NSUTF8StringEncoding];
}
@end
本文转至:http://blog.sina.com.cn/s/blog_7e6e60490100vg4f.html