ios学习路线—ios沙盒(sandbox)机制和文件操作(二)

1.获取程序的Home目录

NSString *homeDirectory = NSHomeDirectory();  
NSLog(@"path:%@", homeDirectory);  

2.获取document目录

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *path = [paths objectAtIndex:0];  
NSLog(@"path:%@", path); 

3.获取Cache目录

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
NSString *path = [paths objectAtIndex:0];  
NSLog(@"%@", path);

4.获取Library目录

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);  
NSString *path = [paths objectAtIndex:0];  
NSLog(@"%@", path);   

5.获取Tmp目录

NSString *tmpDir = NSTemporaryDirectory();  
NSLog(@"%@", tmpDir);  

6.写入文件

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];  

7.读取文件

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); 
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dubo413392749/article/details/51777181
个人分类: iOS
想对作者说点什么? 我来说一句

史上最好用的沙盒封装Sandbox

2016年01月13日 2KB 下载

iOS Xcode快速打开应用的Sandbox

worldzhy worldzhy

2015-09-25 14:36:45

阅读数:1126

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭