在iOS系统中,每个APP的文件都是独立的, 一个APP大部分处理的文件系统仅限于app的沙盒文件夹.新的APP安装的时候,会产生一系列的文件目录,如下图所示:
下面分开介绍一下:
AppName.app: 包含app和响应的资源等
Documents: 储存用户产生的数据,这个文件夹里面的数据能够被用户所操作,能够被itune备份.
Documents/Inbox: 如果APP允许其他应用打开,这就将其他应用能使用的文件放在这个文件中.
Library: 里面有几个文件夹, Cache:存放缓存, Preference:存放一些配置信息, NSUserDefault的信息就放在这个文件夹里面. 这里面放的文件是不让用户直接操作的数据.
tmp: 临时文件夹,储存一些临时信息,程序退出时会被清除
文件路径:
<pre name="code" class="objc"> //app路径
NSString *appPath = [NSSearchPathForDirectoriesInDomains(NSApplicationDirectory,NSUserDomainMask,YES) objectAtIndex:0];
//Document路径
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//Library路径
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//Library/Cache路径
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//Library/Preferences路径
NSString *preferencePath = [libraryPath stringByAppendingPathComponent:@"Preferences"];
//tmp路径
NSString *tmpPath = NSTemporaryDirectory();
目录相关操作:
NSFileManager *fileManager = [NSFileManager defaultManager];
//判断当前的工作目录
NSString *currentPath = [fileManager currentDirectoryPath];
NSLog(@"currentPath = %@",currentPath); //程序启动后默认是跟目录/(程序的主目录)
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//改变当前目录
if ([fileManager changeCurrentDirectoryPath:documentPath]) {
currentPath = [fileManager currentDirectoryPath];
}
//创建目录
NSString *newDirectory = [documentPath stringByAppendingPathComponent:@"newDir"];
if ([fileManager createDirectoryAtPath:newDirectory withIntermediateDirectories:NO attributes:nil error:nil]) {
NSLog(@"create success"); //create success
}
//列出目录的所有内容
NSArray *fileContent = [fileManager contentsOfDirectoryAtPath:documentPath error:nil];
[fileContent enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"obj = %@",obj); //obj = newDir 文件名字
}];
//删除目录
if ([fileManager removeItemAtPath:newDirectory error:nil]) {
NSLog(@"delete success");//delete succes
}
//目录或者文件信息
NSDictionary *attribute = [fileManager attributesOfItemAtPath:documentPath error:nil];
NSLog(@"attribute = %@",attribute);
/*
attribute = {
NSFileCreationDate = "2014-11-23 09:48:58 +0000";
NSFileExtensionHidden = 0;
NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = staff;
NSFileModificationDate = "2014-11-29 12:57:01 +0000";
NSFileOwnerAccountID = 501;
NSFilePosixPermissions = 493;
NSFileReferenceCount = 2;
NSFileSize = 68;
NSFileSystemFileNumber = 11768762;
NSFileSystemNumber = 16777218;
NSFileType = NSFileTypeDirectory;
}
*/
文件操作:
//判断一个文件是否存在
NSString *filePath = [documentPath stringByAppendingPathComponent:@"newFile.txt"];
if (![fileManager fileExistsAtPath:filePath]) {
[fileManager createFileAtPath:filePath contents:nil attributes:nil];
}
//列出一个目录中的所有文件
NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
NSArray *contents = [fileManager contentsOfDirectoryAtURL:bundleURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
[contents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"obj = %@",obj);
}];
//写入文件
[@"First Date" writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//或者
NSData *data = [@"first data" dataUsingEncoding:NSUTF8StringEncoding];
[data writeToFile:filePath atomically:YES];
//NSFileHandle 取文件操作柄
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSMutableData *orignalData = [NSMutableData dataWithData:[handle readDataToEndOfFile]];
[orignalData appendData:data];
[handle closeFile];
handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
[handle writeData:orignalData];
[handle closeFile];
//删除文件
if ([fileManager removeItemAtPath:filePath error:nil]) {
NSLog(@"delete file success");
}