[Swift通天遁地]七、数据与安全-(6)使用开源类库管理文件夹和操作文件

本文将演示使用开源类库实现创建文件夹和文件操作:创建、写入、移动、遍历。

首先确保在项目中已经安装了所需的第三方库。

点击【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 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10306307.html