- Documents: 存放应用程序产生的数据,会被itunes备份同步
- Library: 包含两个子目录:Caches 和 Preferences
- Preferences:包含应用程序的偏好设置文件。您不应该直接创建偏好设置文件,而是应该使用NSUserDefaults类来取得和设置应用程序的偏好,会被itunes备份同步.
- Caches:用于存放应用程序专用的支持文件,保存应用程序再次启动过程中需要的信息,不会被itunes备份同步。
- tmp: 存放临时数据,当不在用时应该删除掉临时文件,系统也有可能在程序不运行时删除该文件夹下内容
##获取目录方法
###Home目录
//获取沙盒主目录路径
var homeDir=NSHomeDirectory()
println("\(dirHome)")
###Documents目录
//获取Documents目录路径
var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
var documentsDirectory = paths[0] as! String
println("\(documentsDirectory)")
或者相对Home路径
var documentsDirectory = NSHomeDirectory().stringByAppendingPathComponent("Documents")
println("\(documentsDirectory)")
###Library目录
// 获取Library目录路径
var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)
var libraryDirectory = paths[0] as! String
println("\(libraryDirectory)")
或者相对Home路径
var libraryDirectory = NSHomeDirectory().stringByAppendingPathComponent("Library")
println("\(libraryDirectory)")
###Cache目录
// 获取Caches目录路径
var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
var cacheDirectory = paths[0] as! String
println("\(cacheDirectory)")
###tmp目录
// 获取tmp目录路径
var tmpDirectory = NSTemporaryDirectory()
println("\(tmpDirectory)")
或者相对Home目录
var tmpDirectory = NSHomeDirectory().stringByAppendingPathComponent("tmp")
println("\(tmpDirectory)");
##文件操作
###创建文件夹
//在Documents目录下创建test文件夹
var documentDir = NSHomeDirectory().stringByAppendingPathComponent("Documents")
var fileManager = NSFileManager.defaultManager()
var testDir = documentDir.stringByAppendingPathComponent("test")
var error:NSError?=nil
var result = fileManager.createDirectoryAtPath(testDir, withIntermediateDirectories: true, attributes: nil, error: &error)
if result {
println("success")
}else {
println("error:\(error)")
}
###创建文件
//在Documents目录下test文件夹下创建test.txt
var documentDir = NSHomeDirectory().stringByAppendingPathComponent("Documents")
var fileManager = NSFileManager.defaultManager()
var testDir = documentDir.stringByAppendingPathComponent("test")
var fileDir = testDir.stringByAppendingPathComponent("test.txt")
var error:NSError?=nil
var result = fileManager.createFileAtPath(fileDir, contents: nil, attributes: nil)
if result {
println("success")
}else {
println("error:\(error)")
}
###写入文件
var documentDir = NSHomeDirectory().stringByAppendingPathComponent("Documents")
var fileManager = NSFileManager.defaultManager()
var testDir = documentDir.stringByAppendingPathComponent("test")
var fileDir = testDir.stringByAppendingPathComponent("test.txt")
var error:NSError?=nil
var content = "this is a test"
var result = content.writeToFile(fileDir, atomically: true, encoding: NSUTF8StringEncoding, error: &error)
if result {
println("success")
}else {
println("error:\(error)")
}
###读取文件
var documentDir = NSHomeDirectory().stringByAppendingPathComponent("Documents")
var fileManager = NSFileManager.defaultManager()
var testDir = documentDir.stringByAppendingPathComponent("test")
var fileDir = testDir.stringByAppendingPathComponent("test.txt")
var error:NSError?=nil
var content = NSString(contentsOfFile: fileDir, encoding: NSUTF8StringEncoding, error: &error)
if error == nil {
println("success content:\(content!)")
}else {
println("error:\(error)")
}
###读取文件属性
var documentDir = NSHomeDirectory().stringByAppendingPathComponent("Documents")
var fileManager = NSFileManager.defaultManager()
var testDir = documentDir.stringByAppendingPathComponent("test")
var fileDir = testDir.stringByAppendingPathComponent("test.txt")
var error:NSError?=nil
var attrs = fileManager.attributesOfFileSystemForPath(fileDir, error: &error)
if error == nil {
println("success content:\(attrs)")
}else {
println("error:\(error)")
}
###删除文件
var documentDir = NSHomeDirectory().stringByAppendingPathComponent("Documents")
var fileManager = NSFileManager.defaultManager()
var testDir = documentDir.stringByAppendingPathComponent("test")
var fileDir = testDir.stringByAppendingPathComponent("test.txt")
var error:NSError?=nil
var attrs = fileManager.removeItemAtPath(fileDir, error: &error)
if error == nil {
println("success")
}else {
println("error:\(error)")
}
###判断是否为目录
var documentDir = NSHomeDirectory().stringByAppendingPathComponent("Documents")
var fileManager = NSFileManager.defaultManager()
var testDir = documentDir.stringByAppendingPathComponent("test")
var isDir = ObjCBool(false)
var existsDir = fileManager.fileExistsAtPath(testDir, isDirectory:&isDir)
if existsDir {
println("exists Dir")
}else {
println("not exists")
}
留言
張貼留言