您的位置:首页 > 移动开发 > Objective-C

OC文件操作

2012-11-29 12:55 267 查看
对象保存到文件的操作流程:

1、类需要实现<NSCoding>协议

2、在main中进行操作

写入数据流程:

       1)先定义 NSMutableData 对象  data

       2)定义归档、压缩类 NSKeyedArchiver 并用 data进行初始化

       3)encodeObject 编码对象,并指定 key

       4)finishEncoding 完成编码

       5)writeToFile 写入文件

NSMutableData *data = [NSMutableData new];

        //创建归档、压缩的类,用于将数据保存至data中

        NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

        //把要写入的对象进行编码        对对象编码加入关键字

        [arch encodeObject:ft forKey:@"obj-a"];

        [arch finishEncoding];

        

        [data writeToFile:@"/Users/liwei/Desktop/myobj-write.plist" atomically:YES];

读取数据流程:
1)先定义 NSData 对象  data,读取指定路径 data
2)定义解压类 NSKeyedUnarchiver 并用 data进行初始化

       3)根据key 使用 decodeObjectForKey 获取对象

       4)finishDecoding 完成解码

//定义读取数据类对象

        NSData *rdata = [[NSData alloc] initWithContentsOfFile:@"/Users/liwei/Desktop/myobj-write.plist"];

        //解压对象

        NSKeyedUnarchiver *vdUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:rdata];

        //获取写入的对象                               注意 写入时key

        FileTest *ft2 = [vdUnarchiver decodeObjectForKey:@"obj-a"];

        

        [vdUnarchiver finishDecoding];

        

        NSLog(@"%@",ft2.m_s1);

常见的NSFileManger文件方法:

1.-(BOOL) contentsAtpath:path

从一个文件中读取数据

2.-(BOOL) createFileAtPath:path contents:(BOOL)data attributes:attr

向一个文件中写入数据

3.-(BOOL)removeFileAtPath:path handler;handler

删除一个文件

4.-(BOOL)movePath:from toPath:to handler:handler

重命名或者移动一个文件

5.-(BOOL)copyPath:from toPath:to handler:handler

比较这两个文件的内容

6.-(BOOL)fileExistsAtPath:path

测试文件是否存在

7.-(BOOL)isReadableFileAthPath:path

测试文件是否存在,并且是否能执行读操作

8.-(BOOL)isWritableFileAtPath:path

测试文件是否存在,并且是否能执行写操作

9.-(NSDictionary *)fileAttributesAtPath:path traverseLink:(BOOL)flag

获取文件的属性

10.-(BOOL)changeFileAttributes:attr atPath:path

更改文件属性

创建与删除:
//创建文件管理器
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 createFileAtPath:@"fileName" contents:nil attributes:nil];

//删除待删除的文件

[fileManager removeItemAtPath:@"createdNewFile" error:nil];

写入数据:
//获取文件路径
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"fileName"];

//待写入的数据
NSString *temp = @”Welcome to blog.iosxcode4.com”;

int data0 = 100000;

float data1 = 23.45f;

//创建数据缓冲
NSMutableData *writer = [[NSMutableData alloc] init];

//将字符串添加到缓冲中
[writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];

//将其他数据添加到缓冲中
[writer appendBytes:&data0 length:sizeof(data0)];

[writer appendBytes:&data1 length:sizeof(data1)];

//将缓冲的数据写入到文件中
[writer writeToFile:path atomically:YES];

[writer release];

读取数据:
int gData0;

float gData1;

NSString *gData2;

NSData *reader = [NSData dataWithContentsOfFile:path];

gData2 = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])]

encoding:NSUTF8StringEncoding];

[reader getBytes:&gData0 range:NSMakeRange([temp length], sizeof(gData0))];

[reader getBytes:&gData2 range:NSMakeRange([temp length] + sizeof(gData0), sizeof(gData1))];

NSLog(@”gData0:%@  gData1:%i gData2:%f”, gData0, gData1, gData2);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息