標籤:
//Home目錄
let homeDirectory = NSHomeDirectory()
//Documents目錄 蘋果建議將程式中建立的或在程式中瀏覽到的檔案資料儲存在該目錄下,iTunes備份和恢複的時候會包含此目錄
let documentPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let documentPath = documentPaths[0] as! String
//let documentPath = homeDirectory + "/Documents"
//Library目錄 這個目錄下有2個子目錄:Caches緩衝 , Preferences喜好設定,不應該直接建立喜好設定檔案而使用NSUserDefaults類
let libraryPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let libraryPath = libraryPaths[0] as! String
//let libraryPath = homeDirectory + "/Library"
//Cache目錄 主要存放快取檔案,iTunes不會備份此目錄,此目錄下檔案不會在應用退出時刪除
let cachesPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let cachesPath = cachesPaths[0] as! String
//let cachesPath = homeDirectory + "//Library/Caches"
//tmp目錄 用於存放臨時檔案,保持應用程式再次啟動過程中不需要的資訊,重啟後清空
let tmpDir = NSTemporaryDirectory()
//let tmpDir = homeDirectory + "/tmp"
//操作檔案
var fileManager = NSFileManager.defaultManager()
var error : NSError?
let mydir1 = NSHomeDirectory() + "/Documents/myfolder/images"
let mydir2 = NSHomeDirectory() + "/Documents/myfolder/films"
let mydir3 = NSHomeDirectory() + "/Documents/myfolder/musics"
let mydir4 = NSHomeDirectory() + "/Documents/myfolder/files"
//withIntermediateDirectories 設定為true, 代表中間所有的路徑目錄如果不存在,都會建立
var isSuccess : Bool = fileManager.createDirectoryAtPath(mydir1, withIntermediateDirectories: true, attributes: nil, error: &error)
fileManager.createDirectoryAtPath(mydir2, withIntermediateDirectories: true, attributes: nil, error: &error)
fileManager.createDirectoryAtPath(mydir3, withIntermediateDirectories: true, attributes: nil, error: &error)
fileManager.createDirectoryAtPath(mydir4, withIntermediateDirectories: true, attributes: nil, error: &error)
//建立檔案可以通過writeToFile方法將一些對象寫入到檔案中
var filePath = mydir4 + "/appInfo.txt"
var info = "this is test text"
info.writeToFile(filePath, atomically: true, encoding: NSUTF8StringEncoding, error: &error)
//儲存圖片
var image = UIImage(named: "2.jpg")
var data : NSData = UIImageJPEGRepresentation(image, 1.0)
var data1 : NSData = UIImagePNGRepresentation(image)
data.writeToFile(mydir1 + "/2.jpg", atomically: true)
data1.writeToFile(mydir1 + "/2.png", atomically: true)
//儲存NSAarry
var array = NSArray(objects: "111","222","333")
array.writeToFile(mydir4 + "/array.plist", atomically: true)
//儲存NSDictionary
var dic = NSDictionary(objects: ["1111","2222","3333"], forKeys: ["1","2","3"])
dic.writeToFile(mydir4 + "/dictionary.plist", atomically: true)
//判斷目錄是否存在
var isExist = fileManager.fileExistsAtPath(filePath)
//移動檔案
var filePath2 = mydir1 + "/appInfo.txt"
fileManager.moveItemAtPath(filePath, toPath: filePath2, error: &error)
//移動,重新命名
var filePath3 = mydir1 + "/appInfo2.txt"
fileManager.moveItemAtPath(filePath2, toPath: filePath3, error: &error)
//拷貝,重新命名
var filePath4 = mydir1 + "/appInfoCopy.txt"
fileManager.copyItemAtPath(filePath3, toPath: filePath4, error: &error)
//擷取目錄下所有檔案
var fileArray = fileManager.subpathsAtPath(mydir1)
//println(fileArray)
//擷取檔案各項屬性
var fileAttributes : [NSObject : AnyObject]? = NSFileManager.defaultManager().attributesOfItemAtPath(filePath2, error: &error)
var modificationDate = fileAttributes![NSFileModificationDate] as! String
println(modificationDate) //2015-09-03 12:04:39 +0000
//刪除檔案
fileManager.removeItemAtPath(filePath4, error: &error)
//刪除目錄下所有檔案
//方法1
var fileArray2 : [AnyObject]? = fileManager.subpathsAtPath(mydir1)
for f in fileArray2! {
fileManager.removeItemAtPath(mydir1 + "/\(f)", error: &error)
}
//方法2
fileManager.removeItemAtPath(mydir1, error: &error)
fileManager.createDirectoryAtPath(mydir1, withIntermediateDirectories: true, attributes: nil, error: &error)
IOS SWIFT 簡單操作檔案