首先在进行正题之前,先给大家介绍一款软件iFunBox:
它可以查看连接我们Mac的设备的沙盒目录文件夹。看到的目录结构如下:
所以这个是查看我们设备的沙盒目录的方式,那我们如果用模拟器,该如何查看我们的沙盒目录呢?以下介绍两种方式:
1、设置显示隐藏文件:打开终端输入命令:
1)显示隐藏文件:defaults write com.apple.finder AppleShowAllFiles -bool true
2) 隐藏隐藏文件:defaults write com.apple.finder AppleShowAllFiles -bool false
之后,就能看到资源库文件夹了。
2、在Finder上点->前往 然后按住"option"键,就会出现"资源库"。
3、在我们Xcode上我们也可以直接查看到当前运行的设备的沙盒文件:
在Xcode上部的导航栏里,选择window->Devices :我们就可以看到我们自己的设备和沙盒目录了->找到应用双击就可以了;
OK,那我们来看看目录结构:默认情况下,每个沙盒含有3个文件夹:Documents,Library和tmp。应用的沙盒机制,应用只能在几个目录下读写文件。
Documents:建议将程序中浏览到或在程序中浏览到的文件数据保存在该目录下。iTunes备份和恢复的时候会包括此目录;
Library:存储程序的默认设置或者其他状态信息;
Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除;
tmp:提供一个积石山创建临时文件的地方。
iTunes和iPhone同步时,备份所有Documents和library文件
iPhone重启时,丢弃所有tmp文件
上代码:
//1.获取程序的home目录
NSString * homeDictionary = NSHomeDirectory();
NSLog(@"homepath:%@",homeDictionary);
//homepath:/var/mobile/Containers/Data/Application/FF91E63F-B843-478A-986C-BA0FDE78F6FE
//2.获取document目录
NSArray * documentPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSLog(@"documentpath:%@",[documentPathsobjectAtIndex:0]);
//documentpath:/var/mobile/Containers/Data/Application/FF91E63F-B843-478A-986C-BA0FDE78F6FE/Documents
//3.获取Library目录
NSArray * libraryPaths =NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES);
NSLog(@"librarypath:%@",[libraryPathsobjectAtIndex:0]);
//librarypath:/var/mobile/Containers/Data/Application/FF91E63F-B843-478A-986C-BA0FDE78F6FE/Library
//获取tmp目录
NSString * tempDir = NSTemporaryDirectory();
NSLog(@"tmpPath:%@",tempDir);
//tmpPath:/private/var/mobile/Containers/Data/Application/FF91E63F-B843-478A-986C-BA0FDE78F6FE/tmp/
//获取Cache目录
NSArray * cachePaths =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
NSLog(@"cachePath:%@",[cachePathsobjectAtIndex:0]);
//cachePath:/var/mobile/Containers/Data/Application/FF91E63F-B843-478A-986C-BA0FDE78F6FE/Library/Caches"
//写入文件
NSArray * docPath =NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES);
NSString * docDir = [docPath objectAtIndex:0];
if (!docDir) {
NSLog(@"Document目录未找到");
}
NSArray * array = [NSArrayarrayWithObjects:@"内容",@"content",nil];//[[NSArray array] initWithObjects:@"内容",@"content", nil];
NSString * filePath = [docDirstringByAppendingPathComponent:@"testFile.txt"];
[array writeToFile:filePath atomically:YES];
//读文件
NSArray * docPaths =NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES);
NSString * docDir2 = [docPaths objectAtIndex:0];
NSString * filePath2 = [docDir2 stringByAppendingPathComponent:@"testFile2.txt"];
NSArray * array2 = [[NSArrayalloc] initWithContentsOfFile:filePath2];
NSLog(@"%@",array2);
- (void)fileManagerOperation
{
NSFileManager * fileManager = [NSFileManagerdefaultManager];
//获取Documents路径
NSString * documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject];
NSLog(@"documentPath:%@",documentPath);
//创建文件夹
NSString * iosDirectory = [documentPath stringByAppendingPathComponent:@"iOS"];
BOOL isSuccess = [fileManagercreateDirectoryAtPath:iosDirectory withIntermediateDirectories:YESattributes:nilerror:nil];
if (isSuccess) {
NSLog(@"success");
}else{
NSLog(@"fail");
}
//创建文件
NSString * iOSPath = [documentPath stringByAppendingPathComponent:@"iOS.txt"];
BOOL isCreate = [fileManager createFileAtPath:iOSPath contents:nilattributes:nil];
if (isCreate) {
NSLog(@"sucess");
}else{
NSLog(@"fail");
}
//写文件
NSString * content = @"fcf";
BOOL isWrited = [contentwriteToFile:iOSPath atomically:YESencoding:NSUTF8StringEncodingerror:nil];
if (isWrited) {
NSLog(@"success");
}else{
NSLog(@"fail");
}
//读取文件内容
NSLog(@"read success: %@",[NSStringstringWithContentsOfFile:iOSPath encoding:NSUTF8StringEncodingerror:nil]);
//判断文件是否存在
if ([fileManager fileExistsAtPath:iOSPath]) {
NSLog(@"存在");
}else{
NSLog(@"不存在");
}
//计算整个文件所有文件的大小
NSLog(@"%lld",[selffolderSizeAtPath:iOSPath]);
//重命名
NSString * filePath = [documentPath stringByAppendingString:@"iOS.txt"];
NSString * moveToPath = [documentPath stringByAppendingString:@"iOS.txt"];
if ([fileManager moveItemAtPath:filePath toPath:moveToPatherror:nil]) {
NSLog(@"move success");
}
//移动文件
NSString * filePath1 = [documentPath stringByAppendingString:@"iOS.txt"];
NSString * moveToPath1 = [documentPath stringByAppendingString:@"rename.txt"];
if ([fileManager moveItemAtPath:filePath1 toPath:moveToPath1error:nil]) {
NSLog(@"rename success");
}
//删除文件
if ([fileManager removeItemAtPath:iOSPath error:nil]) {
NSLog(@"remove success");
}else{
NSLog(@"fail");
}
}