获取 APP 沙盒目录方式
//1、获取程序的Home目录,在 NSHomeDirectory() 目录下,不能直接创建目录及文件,要在这个目录下的 Documents 目录中才能创建目录及文件
NSString *homeDirectory = NSHomeDirectory();
NSLog(@"path:%@", homeDirectory);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A-4929-B5F2-3E46E41CC671
// 与2同样可以获取 Documents 目录
NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A-4929-B5F2-3E46E41CC671/Documents
//2、获取Documents目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A-4929-B5F2-3E46E41CC671/Documents
//3、获取Cache目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A-4929-B5F2-3E46E41CC671/Library/Caches
//4、获取Library目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A-4929-B5F2-3E46E41CC671/Library
//5、获取tmp目录
NSString *tmpDir = NSTemporaryDirectory();
NSLog(@"path:%@", tmpDir);
//path:/Users/ios/Library/Application Support/iPhone Simulator/6.1/Applications/BF38C9E3-1A4A-4929-B5F2-3E46E41CC671/tmp/
说明:
注:如果APP要使用文档共享,在ios 8.3以后要在 app 配置文件中设置
Application supports iTunes file sharing 为YES
二、文件管理 NSFileManager 类
NSFileManager* fileManager = [NSFileManager defaultManager];
常用函数
createDirectoryAtPath // 创建目录
createFileAtPath // 创建文件【目录要存在,不存在会报错】
removeItemAtPath // 删除文件
fileExistsAtPath // 检查路径或文件是否存在,这个函数重载了一个带 布尔类型的参数,用去返回如果路径或文件存在,是文件还是路径。
contentsAtPath // 读取文件内容
movePath // 移动文件
copyPath // 复制文件
isReadableFileAtPath // 是否可读
isWritableFileAtPath // 是否可写
fileAttributesAtPath // 获取文件属性
changeAttributesAtPath // 改变文件属性
--------------------------------------------------------------------
若有其他凝问或文中有错误,请及时向我指出,
我好及时改正,同时也让我们一起进步。
email : [email protected]
qq : 1035862795
敲门砖: 代码谱写人生