iOS沙盒目录文件操作

简介

沙盒(NSHomeDirectory())中总共有四个文件夹,documentstmpappLibrary;
手动保存的文件在documents文件里;
Nsuserdefaults保存的文件在tmp文件夹里;

Documents目录:

你应该将所有的应用程序数据文件写入到这个目录下。这个目录用于存储用户数据或其它应该定期备份的信息。该路径可通过配置实现iTunes共享文件。可被iTunes备份。

AppName.app目录:

这是应用程序的程序包目录,包含应用程序的本身。由于应用程序必须经过签名,所以您在运行时不能对这个目录中的内容进行修改,否则可能会使应用程序无法启动。

Library目录:

这个目录下有两个子目录CachesPreferences

Preferences 目录:

包含应用程序的偏好设置文件。您不应该直接创建偏好设置文件,而是应该使用NSUserDefaults类来取得和设置应用程序的偏好.

Caches 目录:

用于存放应用程序专用的支持文件,保存应用程序再次启动过程中需要的信息。
可创建子文件夹。可以用来放置您希望被备份但不希望被用户看到的数据。该路径下的文件夹,除Caches以外,都会被iTunes备份。

tmp 目录:

这个目录用于存放临时文件,保存应用程序再次启动过程中不需要的信息。该路径下的文件不会被iTunes备份。

获取这些目录路径的方法:

1,获取根目录路径的函数:

1
NSString *homeDir = NSHomeDirectory();

2,获取Documents目录路径的方法:

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

3,获取Caches目录路径的方法:

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

4,获取tmp目录路径的方法:

1
NSString *tmpDir = NSTemporaryDirectory();

5,获取应用程序程序包中资源文件路径的方法:

例如获取程序包中一个图片资源(apple.png)路径的方法:

1
2
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@”apple” ofType:@”png”];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];

代码中的mainBundle类方法用于返回一个代表应用程序包的对象。

NSFileManager(文件管理)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
NSFileManager* fm=[NSFileManager defaultManager];
if(![fm fileExistsAtPath:[self dataFilePath]]){
//下面是对该文件进行制定路径的保存
[fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil];
//取得一个目录下得所有文件名
NSArray *files = [fm subpathsAtPath: [self dataFilePath] ];
//读取某个文件
NSData *data = [fm contentsAtPath:[self dataFilePath]];
//或者
NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]];
}

删除一个文件目录下的所有该类型的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
NSString *extension = @"log";//清除caches目录下所有log类型的文件
NSArray *contents = [_fileManager contentsOfDirectoryAtPath:cachesDir error:NULL];
NSEnumerator *e = [contents objectEnumerator];
NSString *filename;
while (filename = [e nextObject]) {
if ([[filename pathExtension] isEqualToString:extension]) {
[_fileManager removeItemAtPath:[cachesDir stringByAppendingPathComponent:filename] error:NULL];
}
}

统计caches目录下所有log文件的总大小

1
2
3
4
5
6
7
8
9
10
_IMCacheSize = 0;
while (filename = [e nextObject]) {
if ([[filename pathExtension] isEqualToString:extension]) {
NSDictionary *IMfileDic = [_fileManager attributesOfItemAtPath:cachesDir error:NULL];
_IMCacheSize += IMfileDic.fileSize/1024.0;
}
}

获取Documents目录路径的方法:

1
2
3
4
5
6
7
8
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
//取得一个目录下得所有文件名
NSArray *files = [fm subpathsAtPath: docDir];
NSLog(@"files = %@",files);
文件重命名:(利用fm移动到当前路径,替换文件名即可)
[_fileManager moveItemAtPath:_path toPath:newPath error:nil];