码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-15 13:17:39      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

 

----------------------------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];

}

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

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

标签:

原文地址:http://www.cnblogs.com/woshihuaidan/p/4732073.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!