IOS对文件操作包含文件的创建,读写,移动,删除等操作。
1.文件的创建:
//设定文本框存储文件的位置
NSString *strFilePath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
//指定存储文件的文件名
NSString *fileName=[strFilePath stringByAppendingPathComponent:@"test.txt"];
//文件管理器<pre name="code" class="objc">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 <Foundation/Foundation.h>
@interface Person : NSObject<NSCoding>
@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);
}