oc中对文件的操作(加载已有文件数据,存取文件)

 

----------------------------File.h-------------------------------

#import <Foundation/Foundation.h>

 

@interface FileHelper : NSObject

+(NSMutableArray *)loadData;//加载数据并保存在数组中

+(void)saveStudentData:(NSMutableArray *)dataArr;//保存数据在数组中

+(NSMutableArray *)loadStudentData;//以学生为例

@end

----------------------------------------------------------------

----------------------------File.m-----------------------------

#import "File.h"

/**设置文件保存的路径*/

static NSString * DATA_INFO_FILE = (NSString *)path;

 

@implementation File

//加载文件,

+(NSMutableArray *)loadDataFromFile:(NSString *)filename{

    return [NSKeyedUnarchiver unarchiveObjectWithFile:filename];

}

 //保存文件

+(void)saveData:(NSMutableArray *)dataArr toFile:(NSString *)filename{

    [NSKeyedArchiver archiveRootObject:dataArr toFile:filename];

}

//加载学生数据

+(NSMutableArray *)loadStudentData{

    // self 就是指调用当前方法的receiver

    // 谁调用当前方法,self就指谁

   return [self loadDataFromFile:STUDENT_INFO_FILE];

}

//保存学生数据

+(void)saveStudentData:(NSMutableArray *)dataArr{

    [self saveData:dataArr toFile:STUDENT_INFO_FILE];

}

----------------------------------------------------------------

----------------------Student---------------------------------

#import <Foundation/Foundation.h>

 

@interface Student : NSObject<NSCoding>

//学号、姓名、密码、系别、班级、电话

@property(nonatomic, copy)NSString * number;

@property(nonatomic, copy)NSString * name;

@property(nonatomic, copy)NSString * password;

@property(nonatomic, copy)NSString * department;

@property(nonatomic, copy)NSString * grade;

@property(nonatomic, copy)NSString * phone;

-(id)initWithNumber:(NSString *)number andName:(NSString *)name andPassword:(NSString *)password andDepartment:(NSString *)department andGrade:(NSString *)grade andPhone:(NSString *)phone;

@end

-------------------------------------------------------------

-----------------------Student.m--------------------------

#import "Student.h"

@implementation Student

 /**初始化学生对象*/

-(id)initWithNumber:(NSString *)number andName:(NSString *)name andPassword:(NSString *)password andDepartment:(NSString *)department andGrade:(NSString *)grade andPhone:(NSString *)phone{

    self = [super init];

    if (self) {

        self.number = number;

        self.name = name;

        self.password = password;

        self.department = department;

        self.grade = grade;

        self.phone = phone;

    }

    return self;

}

 

/**序列化,作用:通过ios的协议将文件保存*/

- (void)encodeWithCoder:(NSCoder *)aCoder{

    [aCoder encodeObject:self.number forKey:@"number"];

    [aCoder encodeObject:self.name forKey:@"name"];

    [aCoder encodeObject:self.password forKey:@"password"];

    [aCoder encodeObject:self.department forKey:@"department"];

    [aCoder encodeObject:self.grade forKey:@"grade"];

    [aCoder encodeObject:self.phone forKey:@"phone"];

}

 

/**反序列化,作用:通过ios协议读取文件*/

- (id)initWithCoder:(NSCoder *)aDecoder{

    self = [super init];

    if (self) {

        self.number = [aDecoder decodeObjectForKey:@"number"];

        self.name = [aDecoder decodeObjectForKey:@"name"];

        self.password = [aDecoder decodeObjectForKey:@"password"];

        self.department = [aDecoder decodeObjectForKey:@"department"];

        self.grade = [aDecoder decodeObjectForKey:@"grade"];

        self.phone = [aDecoder decodeObjectForKey:@"phone"];

    }

    return self;

}

 

/**打印出学生基本信息*/

-(NSString *)description{

    return [NSString stringWithFormat:@"%@   %@   %@   %@    %@", self.number, self.name, self.department, self.grade, self.phone];

}

@end

----------------------------------------------------------

---------------Start.h-----------------------------------

+(void)saveDataAfterAppDown;

+(void)loadDataBeforeAppRun;

----------------------------------------------------------

----------------------Start.m---------------------------

+(void)loadDataBeforeAppRun

{

/**加载学生信息文件并初始化一个学生信息数组,用来保存学生的基本信息*/

    

    studentArr = [FileHelper loadStudentData];

    if (studentArr == nil) {

        studentArr = [[NSMutableArray alloc]init];

    }

}

 

/**保存本次的数据*/

+(void)saveDataAfterAppDown{

    NSLog(@"系统退出,正在保存信息。。。");

    

    [File saveStudentData:studentArr];

}

----------------------------------------------------------

转载于:https://www.cnblogs.com/woshihuaidan/p/4732073.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值