IOS資料處理及版本特性-對檔案,資料夾的操作
阿新 • • 發佈:2018-11-22
// // ViewController.swift // DemoApp // // Created by 郭文亮 on 2018/11/19. // Copyright © 2018年 finalliang. All rights reserveds // import UIKit class ViewController: UIViewController{ override func viewDidLoad() { super.viewDidLoad() //建立資料夾 //獲取檔案管理物件:讀寫檔案資料 刪除複製移動檔案 比較檔案內容 let manager = FileManager.default //常見自付出啊你物件 該字串表示了文件目錄下的第一個檔案 let baseUrl = NSHomeDirectory()+"/Documents/txtFolder" //判斷資料夾是否存在 let exist = manager.fileExists(atPath: baseUrl) if !exist { //如果檔案不存在就建立一個新的資料夾 do{ try manager.createDirectory(atPath: baseUrl, withIntermediateDirectories: true, attributes: nil) print("txtFolder建立成功") }catch{ print("Error to create folder") } }else{ print("txtFolder已存在") } writeText() writeArray() writeDictionary() writeImage() test1() copyFile() moveFile() removeFile() test1() } //複製檔案 func copyFile() { let fileManager = FileManager.default //建立一個字串物件表示文件目錄下的一個檔案 //在建立一個 表示被複制後的目標位置 let sourceURL = NSHomeDirectory()+"/Documents/swift.txt" let targetURL = NSHomeDirectory()+"/Documents/swift_bak.txt" //複製檔案 do { try fileManager.copyItem(atPath: sourceURL, toPath: targetURL) print("複製成功") } catch { print("複製異常") } } //移動檔案 func moveFile() { let fileManager = FileManager.default let sourceURL = NSHomeDirectory()+"/Documents/productsPath.plist" let targetURL = NSHomeDirectory()+"/Documents/productsPath_bak.plist" //移動檔案 do { try fileManager.moveItem(atPath: sourceURL, toPath: targetURL) print("移動成功") } catch { print("移動異常") } } //刪除檔案 func removeFile() { let fileManager = FileManager.default let sourceURL = NSHomeDirectory()+"/Documents/myPic.png" //刪除檔案 do { try fileManager.removeItem(atPath: sourceURL) print("刪除成功") } catch { print("刪除異常") } } //對資料夾進行遍歷操作 func test1() { let manager = FileManager.default let url = NSHomeDirectory()+"/Documents/" do { //獲取文件目錄下的所有內容 並存儲在一個數組物件中 let contents = try manager.contentsOfDirectory(atPath: url) print("contents:\(contents)")//陣列內容 //獲取文件目錄下的所有內容以及自資料夾的宿友內容 並存儲在一個數組物件中 let contents2 = manager.enumerator(atPath: url) print("contents2:\(String(describing: contents2?.allObjects))")//陣列內容 } catch { print(error) } } //建立文字檔案 func writeText() { let filePath:String = NSHomeDirectory()+"/Documents/swift.txt" let info = "這裡是文字的內容哈哈哈哈哈哈哈哈哈哈哈哈哈哈" do { try info.write(toFile: filePath, atomically: true, encoding: .utf8) print("建立文字swift.txt成功") } catch { print("建立文字swift.txt失敗") } } //將陣列儲存為屬性列表檔案 func writeArray() { let fruits = NSArray(objects: "apple","banana","orange") let fruitsPath:String = NSHomeDirectory()+"/Documents/fruitsPath.plist" fruits.write(toFile: fruitsPath, atomically: true) print("建立文字fruitsPath.plist成功") } //將字典物件儲存為屬性列表檔案 func writeDictionary() { var dictionary:Dictionary <String,String> dictionary = ["software":"xcode","language":"swift"] let products = dictionary as NSDictionary let productsPath:String = NSHomeDirectory()+"/Documents/productsPath.plist" products.write(toFile: productsPath, atomically: true) print("建立文字productsPath.plist成功") } //儲存圖片檔案 func writeImage() { let imagePath: String = NSHomeDirectory()+"/Documents/myPic.png" let image = UIImage(named: "Pic4") //將圖片內容壓縮轉換成二進位制內容 let imagedata:Data = UIImagePNGRepresentation(image!)! try? imagedata.write(to: URL(fileURLWithPath: imagePath), options: [.atomic]) print("建立文字myPic.png成功") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }