1. 程式人生 > >[Swift通天遁地]七、數據與安全-(6)使用開源類庫管理文件夾和操作文件

[Swift通天遁地]七、數據與安全-(6)使用開源類庫管理文件夾和操作文件

添加 第三方類庫 兩個 三方 class miss platform res 表示

本文將演示使用開源類庫實現創建文件夾和文件操作:創建、寫入、移動、遍歷。

首先確保在項目中已經安裝了所需的第三方庫。

點擊【Podfile】,查看安裝配置文件。

1 platform :ios, 12.0
2 use_frameworks!
3 
4 target DemoApp do
5     source https://github.com/CocoaPods/Specs.git
6     pod FileKit
7 end

根據配置文件中的相關配置,安裝第三方庫。

在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】

  1 import UIKit
  2 //引入已經安裝的第三方類庫
  3 import FileKit
  4 
  5 class ViewController: UIViewController {
  6 
  7     override func viewDidLoad() {
  8         super.viewDidLoad()
  9         // Do any additional setup after loading the view, typically from a nib.
 10         //創建文件
 11         createFile()
12 //創建一個文件夾 13 createDirectory() 14 //復制一個文件 15 copyFile() 16 //移動一個文件 17 moveFile() 18 //遍歷一個文件夾下的所有項目 19 findPath() 20 //寫入文件 21 writeFile() 22 } 23 24 //添加一個方法,創建文件 25 func createFile() 26
{ 27 //添加一個異常捕捉語句, 28 //用來在沙箱目錄中,創建一個文件。 29 do 30 { 31 //初始化一個字符串常量,表示文件所在的沙箱路徑 32 let file = NSHomeDirectory() + "/Documents/firstFile.txt" 33 //使用字符串生成一個路徑對象 34 let path = Path(file) 35 //調用路徑對象創建文件的方法, 36 //在指定路徑上創建一個文本文件。 37 try path.createFile() 38 //在控制臺輸出文件的絕對路徑 39 print(path.absolute) 40 } 41 catch 42 { 43 print("Something went wrong :(") 44 } 45 } 46 47 //添加一個方法,創建一個文件夾 48 func createDirectory() 49 { 50 //添加一個異常捕捉語句, 51 //用來在沙箱目錄中,創建一個文件。 52 do 53 { 54 //初始化兩個字符串常量,表示文件夾所在的沙箱路徑 55 let directory1 = NSHomeDirectory() + "/Documents/myFolder1/subFolder" 56 let directory2 = NSHomeDirectory() + "/Documents/myFolder2" 57 58 //使用字符串生成一個路徑對象 59 let path1 = Path(directory1) 60 //調用路徑對象創建文件夾的方法, 61 //在指定路徑上創建一個文件夾。包含子文件夾【subFolder】 62 try path1.createDirectory() 63 64 //使用字符串生成一個路徑對象 65 let path2 = Path(directory2) 66 //調用路徑對象創建文件夾的方法, 67 //在指定路徑上創建一個文件夾。 68 //並且不創建之間的子文件夾。 69 try path2.createDirectory(withIntermediateDirectories: false) 70 71 //在控制臺輸出兩個文件夾的路徑。 72 print(path1) 73 print(path2) 74 } 75 catch 76 { 77 print("Something went wrong :(") 78 } 79 } 80 81 //添加一個方法,復制一個文件 82 func copyFile() 83 { 84 //初始化一個字符串常量,表示文件所在的沙箱路徑。 85 let file1 = NSHomeDirectory() + "/Documents/firstFile.txt" 86 //初始化另一個字符串常量,表示文件被復制到的目標位置。 87 let file2 = NSHomeDirectory() + "/Documents/myFolder1/subFolder/firstFile_bak.txt" 88 89 //依次將兩個字符串,轉換成路徑對象。 90 let path1 = Path(file1) 91 let path2 = Path(file2) 92 93 //添加一個異常捕捉語句,用來執行文件的復制。 94 do 95 { 96 //通過調用路徑對象的復制到方法, 97 //將指定位置的文件,復制到另一個位置。 98 try path1.copyFile(to: path2) 99 } 100 catch 101 { 102 print("Something went wrong :(") 103 } 104 } 105 106 //添加一個方法,移動一個文件 107 func moveFile() 108 { 109 //初始化一個字符串常量,表示文件所在的沙箱路徑。 110 let file1 = NSHomeDirectory() + "/Documents/firstFile.txt" 111 //初始化另一個字符串常量,表示文件被移動到的目標位置。 112 let file2 = NSHomeDirectory() + "/Documents/myFolder1/subFolder/firstFile_moved.txt" 113 114 //依次將兩個字符串,轉換成路徑對象。 115 let path1 = Path(file1) 116 let path2 = Path(file2) 117 118 //添加一個異常捕捉語句, 119 do 120 { 121 //通過調用路徑對象的移動到方法, 122 //將指定位置的文件,移動到另一個位置。 123 try path1.moveFile(to: path2) 124 } 125 catch 126 { 127 print("Something went wrong :(") 128 } 129 } 130 131 //添加一個方法,遍歷一個文件夾下的所有項目 132 func findPath() 133 { 134 //初始化一個字符串常量,表示文件夾所在的沙箱路徑。 135 let directory = NSHomeDirectory() + "/Documents/myFolder1/subFolder" 136 //將字符串轉換成路徑對象 137 let path = Path(directory) 138 139 //通過調用路徑對象的查找方法, 140 //查找最多5層子目錄中的所有文本文件。 141 let textFiles = path.find(searchDepth: 5) { path in 142 path.pathExtension == "txt" 143 } 144 145 //對獲得的項目列表進行遍歷 146 for file in textFiles 147 { 148 //絕對路徑 149 print(">>>> file path: \(file.absolute)") 150 //文件名稱 151 print(">>>> file name: \(file.fileName)") 152 //文件類型 153 print(">>>> file type: \(String(describing: file.fileType))") 154 //文件大小 155 print(">>>> file size: \(String(describing: file.fileSize))") 156 //是否存在 157 print(">>>> file exists: \(file.exists)") 158 //父目錄 159 print(">>>> file parent: \(file.parent)") 160 //可寫權限 161 print(">>>> file isWritable: \(file.isWritable)") 162 //可讀權限 163 print(">>>> file isReadable: \(file.isReadable)") 164 //可刪除權限 165 print(">>>> file isDeletable: \(file.isDeletable)") 166 //是否為文件夾 167 print(">>>> file isDirectory: \(file.isDirectory)") 168 //項目權限信息 169 print(">>>> file filePermissions: \(file.filePermissions)") 170 //是否為指定路徑的子項目 171 print(">>>> file isChildOfPath: \(file.isChildOfPath(path))") 172 } 173 } 174 175 //添加一個方法, 176 //往一個已經存在的文本文件中,寫入新的內容。 177 func writeFile() 178 { 179 //添加一個異常捕捉語句,用來執行寫入文件的操作 180 do 181 { 182 //初始化一個字符串常量,表示文件所在的沙箱路徑。 183 let file = NSHomeDirectory() + "/Documents/myFolder1/subFolder/firstFile_moved.txt" 184 //將字符串轉換成路徑對象 185 let path = Path(file) 186 187 //將字符串寫入到指定路徑的文本文件中 188 try "Here is CoolKeTang." |> TextFile(path: path) 189 } 190 catch 191 { 192 print("I can‘t write to a file?!") 193 } 194 } 195 196 override func didReceiveMemoryWarning() { 197 super.didReceiveMemoryWarning() 198 // Dispose of any resources that can be recreated. 199 } 200 }

[Swift通天遁地]七、數據與安全-(6)使用開源類庫管理文件夾和操作文件