iOS iPhone 开发中的文件读写及数据存储

来源:互联网 发布:系统优化对比报告 编辑:程序博客网 时间:2024/05/01 04:23

数据存储无疑软件开发重要课题。本文初学者介绍iphone开发常见文件读写

iOS的文件存储采用的是“沙箱机制”,也就是应用程序只能访问自己的文件目录,每个应用程序的数据是独立的,就像一个一个的沙箱一样。这种管理方法windows原来塞班那种文件管理方式适合移动平台。这种方式安全,很大程度上避免了流氓软件垃圾软件盗窃资料。

查看模拟器应用程序在mac上的存储,就可以了解在iphone上文件是如何组织的。

打开目录/Users/andy/Library/Application Support/iPhone Simulator/5.1/Applications就会看到模拟器上的程序文件夹,你会看到iphone每个应用程序都有自己的3个目录(Document,Library,tmp)

Documents存放应用程序的数据。

Library目录下面还有Preferences和Caches目录,Preferences目录存放应用程序的使用偏好,Caches目录与Documents很相 是可以存放应用程序的数据。

tmp目录供应用程序存储临时文件。


注意,如果你的设置没有设置为查看隐藏目录,你是看不到的,设置显示隐藏文件方法:在终端输入命令:defaults write com.apple.finder AppleShowAllFiles -bool true然后重启下finder。 


在应用程序中获得自己的documents目录:

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

在上面的基础上,获得一个完整的文件路径和名字:

NSString * file = [documentDirectory stringByAppendingPathComponent:@"file1.txt"];

这就可以用file来创建,读取,和写入文件。


iOS中数据存储常见的有四种方式: 属性列表、对象归档、ios嵌入式数据库(SQLite3)和Core Data(苹果提供的工具)

一,用属性列表保存数据:

该方法就是针对一些集合类调用writeToFile:atomically方法和initWithContentsOfFile 方法来写入和读取数据。

这些集合类包括:NSArray,NSMutableArray,NSDictionary,NSMutableDictionary,NSData,NSMutableData,NSString,NSMutableString,NSNumber,NSDate。在这里输入代码太麻烦了,无法识别回车换行,所以这个就不在这里举例了。

二,归档方法保存自定义对象:

属性列表方法简单易用,但是使用有局限性,就是无法保存自定义的数据类。要解决这个问题,我们看归档方法。归档方法实际就是 用 NSKeyedArchiver 对 自定义类进行编解码成 NSMutableData 后,再对NSMutableData实行序列化具体的编解码是由NSCoder实现的。举个例子就比较容易掌握。

例子中我们保存4个值和一个名字字典。这些名字包括昵称,qq网名,微博网名,省份证名字。自定义类如下:

头文件:

#import <Foundation/Foundation.h> 

@interface BIDFourLines : NSObject

@property (copy, nonatomic) NSString *field1;

@property (copy, nonatomic) NSString *field2;

@property (copy, nonatomic) NSString *field3;

@property (copy, nonatomic) NSString *field4;

@property (copy, nonatomic) NSDictionary * names;

@end 


实现文件:

#import "BIDFourLines.h"

#define kField1Key @"Field1"

#define kField2Key @"Field2"

#define kField3Key @"Field3"

#define kField4Key @"Field4"

#define kFieldArrayKey @"FieldArray"

#pragma mark NSCoding

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

 [encoder encodeObject:field1 forKey:kField1Key];

[encoder encodeObject:field2 forKey:kField2Key];

[encoder encodeObject:field3 forKey:kField3Key];

[encoder encodeObject:field4 forKey:kField4Key];

[encoder encodeObject:names forKey:kFieldArrayKey];

}

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

if (self = [super init]) {

field1 = [decoder decodeObjectForKey:kField1Key];

field2 = [decoder decodeObjectForKey:kField2Key];

field3 = [decoder decodeObjectForKey:kField3Key];

field4 = [decoder decodeObjectForKey:kField4Key];

names = [decoder decodeObjectForKey:kFieldArrayKey];

}

return self;

}

- (id)copyWithZone:(NSZone *)zone {

BIDFourLines *copy = [[[self class] allocWithZone: zone] init];

copy.field1 = [self.field1 copyWithZone:zone];

copy.field2 = [self.field2 copyWithZone:zone];

copy.field3 = [self.field3 copyWithZone:zone];

copy.field4 = [self.field4 copyWithZone:zone];

copy.names = [self.names copyWithZone:zone];

return copy;

}

@end


保存文件代码:

- (void) saveTofile{

BIDFourLines *fourLines = [[BIDFourLines alloc] init];

fourLines.field1 = field1.text;

fourLines.field2 = field2.text;

fourLines.field3 = field3.text;

fourLines.field4 = field4.text;

fourLines.names = [NSDictionary dictionaryWithObjectsAndKeys:@"andy",@"nickName",@"王勃",@"idName",@"田鼠",@"qqName",@"大力哥",@"weiboName",nil ]; NSMutableData *data = [[NSMutableData alloc] init];

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

[archiver encodeObject:fourLines forKey:kDataKey];

[archiver finishEncoding];

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

}

获取文件名函数:

- (NSString *)dataFilePath {

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

return [documentsDirectory stringByAppendingPathComponent:@"archive"];

}


- (void) loadFileData{

NSString *filePath = [self dataFilePath];

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];

NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

BIDFourLines *fourLines = [unarchiver decodeObjectForKey:kDataKey];

[unarchiver finishDecoding];

field1.text = fourLines.field1;

field2.text = fourLines.field2;

field3.text = fourLines.field3;

field4.text = fourLines.field4;

NSDictionary * names = fourLines.names;

if(names) {

NSArray * array = [names allKeys];

  for (NSString * value in array) {

     NSLog(@"%@ is %@",value,[names objectForKey:value]);

   }  

  }

 }

}


0 0