iOS沙盒-NSBundle

2018-12-01  本文已影响34人  吃蘑菇De大灰狼

沙盒NSBundle

iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等。
NSBundle包,是一种特定的文件类型,一个主要作用是 获取Resources文件夹中的资源
[NSBundle mainBundle]是获得NSBundle的一个单例对象,次单例对象 已经设置了默认的resourcePath,也就是你的app打包后的路径,

沙盒目录结构

默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。因为应用的沙盒机制,应用只能在几个目录下读写文件

  1. 获取程序的Home目录
NSArray 
NSString *homeDirectory = NSHomeDirectory();  
NSLog(@"path:%@", homeDirectory);  
  1. 获取document目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *path = [paths objectAtIndex:0];  
NSLog(@"path:%@", path);   
  1. 获取Cache目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
NSString *path = [paths objectAtIndex:0];  
NSLog(@"%@", path);  
  1. 获取Library目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);  
NSString *path = [paths objectAtIndex:0];  
NSLog(@"%@", path);  
  1. 获取Tmp目录
NSString *tmpDir = NSTemporaryDirectory();  
NSLog(@"%@", tmpDir); 
  1. 写入文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *docDir = [paths objectAtIndex:0];  
if (!docDir) {  
    NSLog(@"Documents 目录未找到");          
}  
NSArray *array = [[NSArray alloc] initWithObjects:@"内容",@"content",nil];  
NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];  
[array writeToFile:filePath atomically:YES];
  1. 读取文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *docDir = [paths objectAtIndex:0];  
NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];  
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];  
NSLog(@"%@", array);  

NSFileManager

文件管理器

0、判断一个文件是否存在,传入全路径(fileExistsAtPath)

// 创建文件管理器
NSFileManager * fileManager = [NSFileManager defaultManager];

NSString * documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString * filePath = [documents stringByAppendingPathComponent:@"test"];

// 判断一个文件是否存在,传入全路径
if ([fileManager fileExistsAtPath:filePath]) {
    NSLog(@"it is exit");
}

1、在Documents里创建目录

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
NSString *documentsDirectory = [paths objectAtIndex:0];    
NSLog(@"documentsDirectory%@",documentsDirectory);    
NSFileManager *fileManager = [NSFileManager defaultManager];    
NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];    
// 创建目录  
[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];  

2、在目录下创建文件

NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test00.txt"];    
NSString *testPath2 = [testDirectory stringByAppendingPathComponent:@"test22.txt"];    
NSString *testPath3 = [testDirectory stringByAppendingPathComponent:@"test33.txt"];    

NSString *string = @"写入内容,write String";  
[fileManager createFileAtPath:testPath contents:[string  dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];  
[fileManager createFileAtPath:testPath2 contents:[string  dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];  
[fileManager createFileAtPath:testPath3 contents:[string  dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];  

3、获取目录列里所有文件名

两种方法获取:subpathsOfDirectoryAtPath 和subpathsAtPath

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
NSString *documentsDirectory = [paths objectAtIndex:0];    
NSLog(@"documentsDirectory%@",documentsDirectory);    
NSFileManager *fileManage = [NSFileManager defaultManager];    
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];    
NSArray *file = [fileManage subpathsOfDirectoryAtPath: myDirectory error:nil];   
NSLog(@"%@",file);    
NSArray *files = [fileManage subpathsAtPath: myDirectory ];   
NSLog(@"%@",files);  

4、fileManager使用操作当前目录

//创建文件管理器  
NSFileManager *fileManager = [NSFileManager defaultManager];  
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsDirectory = [paths objectAtIndex:0];  
//更改到待操作的目录下  
[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];  
//创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil  
NSString * fileName = @"testFileNSFileManager.txt";  
NSArray *array = [[NSArray alloc] initWithObjects:@"hello world",@"hello world1", @"hello world2",nil];  
[fileManager createFileAtPath:fileName contents:array attributes:nil]; 

5、删除文件

[fileManager removeItemAtPath:fileName error:nil];  

参考

  1. 沙盒机制和文件(三) · iOS 高级 · 看云
上一篇下一篇

猜你喜欢

热点阅读