您的位置:首页 > 其它

[数据存储]概述以及文件FileManager

2013-06-26 11:17 127 查看
在开发过程中,必不可少的需要把数据保存在本地,以提高效率和增加用户体验。ios中的数据存储大体分为以下几种,下面分别介绍下,每种方法有自己的优势,也有不足的地方,具体选哪种方法要根据实际需求随机应变。

1.
直接写文件FileManager。

2.
使用系统提供的NSUserDefaults 或者 NSKeyedArchiver。

3.
属性列表plist。

4.
CoreData.

5.
Sqlite数据库。

6.
KeyChain存储

首先说一下数据存储的位置,

/Users/$your user name$/Library/Application Support/iPhone Simulator/6.0/Applications

IOS应用程序采用沙盒原理设计,ios每个应用程序都有自己的3个目录(Document,Library,tmp),互相之间不能访问。

home目录。

NSLog(@"home path:%@", NSHomeDirectory());

Documents存放应用程序的数据。

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

Library目录下面还有Preferences和Caches目录,Preferences目录存放应用程序的使用偏好,Caches目录与Documents很相 似可以存放应用程序的数据。(基于NSUserDefault首选项设置存储在其下Preferences文件夹中)

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


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

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

NSLog(@"tem path:%@", NSTemporaryDirectory());

读写文件的操作有多种方法,ios内置对象几乎都可以直接写到文件中,比如说NSString, NSArray, NSDictionary, NSData等。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [paths objectAtIndex:0];
NSString *content = @"file content.";
NSString *filePath = [docPath stringByAppendingPathComponent:@"string.txt"];
[content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

NSArray *array = @[@"1",@"2"];
NSString *filePathArray = [docPath stringByAppendingPathComponent:@"array.txt"];
[array writeToFile:filePathArray atomically:YES];

NSDictionary *dictionary = @{@"key1" : @"object1", @"key2" : @"object2"};
NSString *filePathDic = [docPath stringByAppendingPathComponent:@"dictionary.txt"];
[dictionary writeToFile:filePathDic atomically:YES];

NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
NSString *filePathData = [docPath stringByAppendingPathComponent:@"data.txt"];
[data writeToFile:filePathData options:NSDataWritingAtomic error:nil];
读文件也是一样,内置对象会有自己相应读文件的初始化方法。
NSLog(@"%@", [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]);
NSLog(@"%@", [NSArray arrayWithContentsOfFile:filePathArray]);
NSLog(@"%@", [NSDictionary dictionaryWithContentsOfFile:filePathDic]);
NSLog(@"%@", [NSData dataWithContentsOfFile:filePathData options:NSUTF8StringEncoding error:nil]);


接下来讲一下ios中统一对文件操作的一个工具NSFileManager。

创建目录:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testDirectory = [docDirectory stringByAppendingPathComponent:@"newFolder"];
[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
创建文件:
[fileManager createFileAtPath:[testDirectory stringByAppendingPathComponent:@"newfile.txt"] contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

读取目录:

//显示目录以及子目录下的文件和文件夹, 不推荐使用
[fileManager subpathsAtPath:docDirectory];
//显示目录以及子目录下的文件和文件夹
[fileManager subpathsOfDirectoryAtPath:docDirectory error:nil];
//显示当前目录下的文件和文件夹,不会遍历子目录
[fileManager contentsOfDirectoryAtPath:docDirectory error:nil];

读取文件:其实apple还是推荐在NSData的方法去获取文件内容。

- (NSData *)contentsAtPath:(NSString *)path;

对文件和目录的操作:

- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
- (BOOL)linkItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
最后,有个很有用的东西:
[fileManager changeCurrentDirectoryPath:[docDirectory stringByExpandingTildeInPath]];
[fileManager createFileAtPath:@"abc.txt" contents:nil attributes:nil];


改变了当前目录后就不用每次添加全路径了。

OK,Done。

所有数据存储的例子:http://download.csdn.net/detail/wanghuafeng123456/5891439
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: