FileManager
IOS应用只能访问自己应用目录下的文件。默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。Library包含Caches、Preferences目录。
let homeDir = NSHomeDirectory()
print("[home]: ", homeDir)
// all sub paths:
// "Documents",
// "Library",
// "Library/Caches/*",
// "Library/Preferences",
// "tmp"
let homeSubDirs = NSFileManager.defaultManager().subpathsAtPath(homeDir)
print("[home sub paths]", homeSubDirs!, homeSubDirs!.count)
// Document
let docDirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
print("[docment]: ", docDirs, docDirs.count)
// Library
let libDirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)
print("[library]: ", libDirs, libDirs.count)
fileManager = NSFileManager.defaultManager()
创建文件:
fileManager.createFileAtPath
或
data.writeToFile(newFileWithPath, atomically: true, encoding: NSUTF8StringEncoding)
创建文件夹
fileManager.createDirectoryAtPath
文件检查
fileManager.fileExistsAtPath(filePath)
移动(重命名)
fileManager.moveItemAtPath
拷贝
fileManager.copyItemAtPath
目录下所有文件(会遍历到所有子目录)
fileManager.subpathsAtPath
删除文件
fileManager.removeItemAtPath