• 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;
    
            }
    
    }
    

      

     

  • 相关阅读:
    学习asp.net比较完整的流程
    时间的获取和转换,C#和Sql
    学习一种新编程语言要做的10几个练习
    分布式网络管理优点总结
    点对点网络大解析
    如何阅读他人的程序代码
    WebConfig
    正则表达式30分钟入门教程
    程序员遇到BUG的解释
    在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)
  • 原文地址:https://www.cnblogs.com/stuwan/p/4363652.html
Copyright © 2020-2023  润新知