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]; }
//寫入文本內容
[self.txtField.text writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"保存成功");
NSFileManager *fmanager=[NSFileManager defaultManager];
if ([fmanager fileExistsAtPath:fileName]) {
[fmanager createFileAtPath:fileName contents:nil attributes:nil];
}
//移動文件
//目標文件夾
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);
}