ios接收java序列化对象_IOS文件操作和自定义对象的归档(序列化)、反归档(反序列化)...

IOS对文件操作包含文件的创建,读写,移动,删除等操作。

1.文件的创建:

//设定文本框存储文件的位置

NSString *strFilePath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];

//指定存储文件的文件名

NSString *fileName=[strFilePath stringByAppendingPathComponent:@"test.txt"];

//文件管理器

NSFileManager *fmaneger=[NSFileManager defaultManager]; if ([fmaneger fileExistsAtPath:[self getFilePath]]) { self.txtField.text=[NSString stringWithContentsOfFile:[self getFilePath] encoding:NSUTF8StringEncoding error:nil]; }

2.文件的写入

//写入文本内容

[self.txtField.text writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:nil];

NSLog(@"保存成功");

3.文件的读取

NSFileManager *fmanager=[NSFileManager defaultManager];

if ([fmanager fileExistsAtPath:fileName]) {

[fmanager createFileAtPath:fileName contents:nil attributes:nil];

}

4.文件的移动

//移动文件

//目标文件夹

NSString *destPath=NSTemporaryDirectory();

//目标文件路径

NSString * destFilePath=[destPath stringByAppendingPathComponent:@"test.txt"];

BOOL result=[fmanager moveItemAtPath:filePath toPath:destFilePath error:nil];

if (result) {

NSLog(@"移动成功");

}

else

{

NSLog(@"移动失败!");

}

5.文件的删除

NSFileManager *fmanager=[NSFileManager defaultManager];

[fmanager removeItemAtPath:filePath error:nil];

文件操作总结:

文件的操作需要先指定文件的路径,这时候就需要用到

NSSearchPathForDirectoriesInDomains

来寻找文件路径。

获取或指定文件路径后,对文件的创建,移动等操作都需要有个NSFileManager对象来实现。

文件的写入方法有多种,基本的有NSString,NSDictionary ,NSArray及其子类都可以调用写入方法将数据写入到文件中去。

下面针对自定义类的对象的文件写入和读取进行演示(即归档/反归档):

步骤:

1。定义一个Person类并让Person类实现NSCoding协议(要实现归档,就必须实现该协议)

//

// Person.h

// 文件操作

//

// Created by lifewahaha on 15/5/15.

// Copyright (c) 2015年 lifewahaha. All rights reserved.

//

#import

@interface Person : NSObject

@property(strong,nonatomic)NSString *name;

@property(nonatomic)int age;

-initWithName:(NSString *)name;

@end

2. Person类的实现文件里要实现两个协议方法

-(void)encodeWithCoder:(NSCoder *)aCoder

-(id)initWithCoder:(NSCoder *)aDecoder

//

// Person.m

// 文件操作

//

// Created by lifewahaha on 15/5/15.

// Copyright (c) 2015年 lifewahaha. All rights reserved.

//

#import "Person.h"

@implementation Person

-(id)initWithName:(NSString *)name

{

if ([super init]) {

self.name=name;

}

return self;

}

#pragma -mark将对象转化为NSData的方法

-(void)encodeWithCoder:(NSCoder *)aCoder

{

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

[aCoder encodeInt:self.age forKey:@"age"];

}

-(id)initWithCoder:(NSCoder *)aDecoder

{

if ([self init]) {

//解压过程

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

self.age=[aDecoder decodeIntForKey:@"age"];

}

return self;

}

@end

3.在ViewController 中导入Person类的头文件,然后再ViewDidLoad中实现如下方法

- (void)viewDidLoad {

[super viewDidLoad];

Person *p=[[Person alloc]init];

p.name=@"a";

p.age=12;

//***********归档,序列化****************

//1.创建一个可变的二进制流

NSMutableData *data=[[NSMutableData alloc]init];

//2.创建一个归档对象(有将自定义类转化为二进制流的功能)

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

//3.用该归档对象,把自定义类的对象,转为二进制流

[archiver encodeObject:p forKey:@"person"];

//4归档完毕

[archiver finishEncoding];

//将data写入文件

[data writeToFile:[self getObjFilePath] atomically:YES];

NSLog(@"%@",data);

//******************反归档******************

NSMutableData *mData=[NSMutableData dataWithContentsOfFile:[self getObjFilePath]];

//2.创建一个反归档对象,将二进制数据解成正行的oc数据

NSKeyedUnarchiver *unArchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:mData];

Person *p2= [unArchiver decodeObjectForKey:@"person"];

NSLog(@"名字:%@",p2.name);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值