IOS 文件,数据的读写

 

NSArray *myPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//NSDocumentDirectory  是表示沙盒里 document 文件夹的列表

//NSDocumentionDirectory  是 获取 沙盒里 library 里 documention里的文件夹列表

    NSString *path=[myPaths objectAtIndex:0]; // objectAtIndex : 为document 的文件列表

    NSString *filename=[path stringByAppendingPathComponent:@"properties.plist"];

 

 

-(IBAction)save:(id)sender

{

    NSArray *myPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *path=[myPaths objectAtIndex:0];

    NSString *filename=[path stringByAppendingPathComponent:@"properties.plist"];

    

    NSMutableArray *array=[[NSMutableArray alloc] init];

    [array addObject:txt_name.text];

    [array addObject:txt_class.text];

    [array addObject:txt_id.text];

    [array writeToFile:filename atomically:YES];

    NSLog(@"come here");

}

-(IBAction)read:(id)sender

{

    NSArray *mypaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *path=[mypaths objectAtIndex:0];

    NSString *filename=[path stringByAppendingPathComponent:@"properties.plist"];

    if([[NSFileManager defaultManager] fileExistsAtPath:filename])

    {

        NSLog(@"it's ok");

        NSMutableArray *array=[[NSMutableArray alloc] initWithContentsOfFile:filename];

        txt_name.text=[array objectAtIndex:0];

        txt_class.text=[array objectAtIndex:1];

        txt_id.text=[array objectAtIndex:2];

    }

    

}

 

以上是保存到 plist 文件里,但是plist 的文件类型有限制,假如是自定义类的话,难以满足需求。

所以有序列化和反序列化 

NSKeyedArchiver  和  NSKeyedUnarchiver

创建一个类  然后  添加两个协议 <NSCoding>

//将对象写入文件中

-(void)encodeWithCoder:(NSCoder *)aCoder

{

    [aCoder encodeObject:studentid forKey:@"stuid"];

    [aCoder encodeObject:studentname forKey:@"stuname"];

    [aCoder encodeObject:studentclass forKey:@"stuclass"];

}

//从文件中读取对象

-(id)initWithCoder:(NSCoder *)aDecoder

{

    self.studentid=[aDecoder decodeObjectForKey:@"stuid"];

    self.studentname=[aDecoder decodeObjectForKey:@"stuname"];

    self.studentclass=[aDecoder decodeObjectForKey:@"stuclass"];

    return self;

}

 

然后在事件中的

-(IBAction)save:(id)sender

{

NSArray *myPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *path=[myPaths objectAtIndex:0];

    NSString *filename=[path stringByAppendingPathComponent:@"student.archive"];

    NSMutableData *data=[NSMutableData data];

    //NSkeyedArchiver 是将 data 变成 二进制 文件

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

    student *stu=[[student alloc] init];

    stu.studentid=txt_id.text;

    stu.studentname=txt_name.text;

    stu.studentclass=txt_class.text;

    //写入到文件

    NSLog(@"%@",stu.studentname);

    [archiver encodeObject:stu forKey:@"stu"]; //  encodeObject:stu 是调用了 上面的 encodeObject :aCoder  方法

    [archiver finishEncoding];

    [data writeToFile:filename atomically:YES];

    NSLog(@"%@",data);

}

 

-(IBAction)read:(id)sender

{

 

  NSArray *myPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *path=[myPaths objectAtIndex:0];

    NSString *filename=[path stringByAppendingPathComponent:@"student.archive"];

    

        NSData *data=[NSData dataWithContentsOfFile:filename];

        NSLog(@"%@",data);

        if([data length]>0)

        {

            NSKeyedUnarchiver * archiver=[[NSKeyedUnarchiver alloc]initForReadingWithData :data];

            student *stu= [archiver decodeObjectForKey:@"stu"];   //decodeObjectForKey 是调用上面 decodeObjectForKey:aCoder方法

            txt_name.text=stu.studentname;

            txt_id.text=stu.studentid;

            txt_class.text=stu.studentclass;

        }

}

  

 

posted @ 2015-03-24 18:34  煮水丶  阅读(178)  评论(0编辑  收藏  举报