本文将演示使用开源类库实现创建文件夹和文件操作: 创建, 写入, 移动, 遍历.
首先确保在项目中已经安装了所需的第三方库.
点击 [Podfile] , 查看安装配置文件.
- platform :iOS, '12.0'
- use_frameworks!
- target 'DemoApp' do
- source 'https://github.com/CocoaPods/Specs.git'
- pod 'FileKit'
- end
根据配置文件中的相关配置, 安装第三方库.
在项目导航区, 打开视图控制器的代码文件 [ViewController.swift]
- import UIKit
- // 引入已经安装的第三方类库
- import FileKit
- class ViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- // 创建文件
- createFile()
- // 创建一个文件夹
- createDirectory()
- // 复制一个文件
- copyFile()
- // 移动一个文件
- moveFile()
- // 遍历一个文件夹下的所有项目
- findPath()
- // 写入文件
- writeFile()
- }
- // 添加一个方法, 创建文件
- func createFile()
- {
- // 添加一个异常捕捉语句,
- // 用来在沙箱目录中, 创建一个文件.
- do
- {
- // 初始化一个字符串常量, 表示文件所在的沙箱路径
- let file = NSHomeDirectory() + "/Documents/firstFile.txt"
- // 使用字符串生成一个路径对象
- let path = Path(file)
- // 调用路径对象创建文件的方法,
- // 在指定路径上创建一个文本文件.
- try path.createFile()
- // 在控制台输出文件的绝对路径
- print(path.absolute)
- }
- catch
- {
- print("Something went wrong :(")
- }
- }
- // 添加一个方法, 创建一个文件夹
- func createDirectory()
- {
- // 添加一个异常捕捉语句,
- // 用来在沙箱目录中, 创建一个文件.
- do
- {
- // 初始化两个字符串常量, 表示文件夹所在的沙箱路径
- let directory1 = NSHomeDirectory() + "/Documents/myFolder1/subFolder"
- let directory2 = NSHomeDirectory() + "/Documents/myFolder2"
- // 使用字符串生成一个路径对象
- let path1 = Path(directory1)
- // 调用路径对象创建文件夹的方法,
- // 在指定路径上创建一个文件夹. 包含子文件夹 [subFolder]
- try path1.createDirectory()
- // 使用字符串生成一个路径对象
- let path2 = Path(directory2)
- // 调用路径对象创建文件夹的方法,
- // 在指定路径上创建一个文件夹.
- // 并且不创建之间的子文件夹.
- try path2.createDirectory(withIntermediateDirectories: false)
- // 在控制台输出两个文件夹的路径.
- print(path1)
- print(path2)
- }
- catch
- {
- print("Something went wrong :(")
- }
- }
- // 添加一个方法, 复制一个文件
- func copyFile()
- {
- // 初始化一个字符串常量, 表示文件所在的沙箱路径.
- let file1 = NSHomeDirectory() + "/Documents/firstFile.txt"
- // 初始化另一个字符串常量, 表示文件被复制到的目标位置.
- let file2 = NSHomeDirectory() + "/Documents/myFolder1/subFolder/firstFile_bak.txt"
- // 依次将两个字符串, 转换成路径对象.
- let path1 = Path(file1)
- let path2 = Path(file2)
- // 添加一个异常捕捉语句, 用来执行文件的复制.
- do
- {
- // 通过调用路径对象的复制到方法,
- // 将指定位置的文件, 复制到另一个位置.
- try path1.copyFile(to: path2)
- }
- catch
- {
- print("Something went wrong :(")
- }
- }
- // 添加一个方法, 移动一个文件
- func moveFile()
- {
- // 初始化一个字符串常量, 表示文件所在的沙箱路径.
- let file1 = NSHomeDirectory() + "/Documents/firstFile.txt"
- // 初始化另一个字符串常量, 表示文件被移动到的目标位置.
- let file2 = NSHomeDirectory() + "/Documents/myFolder1/subFolder/firstFile_moved.txt"
- // 依次将两个字符串, 转换成路径对象.
- let path1 = Path(file1)
- let path2 = Path(file2)
- // 添加一个异常捕捉语句,
- do
- {
- // 通过调用路径对象的移动到方法,
- // 将指定位置的文件, 移动到另一个位置.
- try path1.moveFile(to: path2)
- }
- catch
- {
- print("Something went wrong :(")
- }
- }
- // 添加一个方法, 遍历一个文件夹下的所有项目
- func findPath()
- {
- // 初始化一个字符串常量, 表示文件夹所在的沙箱路径.
- let directory = NSHomeDirectory() + "/Documents/myFolder1/subFolder"
- // 将字符串转换成路径对象
- let path = Path(directory)
- // 通过调用路径对象的查找方法,
- // 查找最多 5 层子目录中的所有文本文件.
- let textFiles = path.find(searchDepth: 5) { path in
- path.pathExtension == "txt"
- }
- // 对获得的项目列表进行遍历
- for file in textFiles
- {
- // 绝对路径
- print(">>>> file path: \(file.absolute)")
- // 文件名称
- print(">>>> file name: \(file.fileName)")
- // 文件类型
- print(">>>> file type: \(String(describing: file.fileType))")
- // 文件大小
- print(">>>> file size: \(String(describing: file.fileSize))")
- // 是否存在
- print(">>>> file exists: \(file.exists)")
- // 父目录
- print(">>>> file parent: \(file.parent)")
- // 可写权限
- print(">>>> file isWritable: \(file.isWritable)")
- // 可读权限
- print(">>>> file isReadable: \(file.isReadable)")
- // 可删除权限
- print(">>>> file isDeletable: \(file.isDeletable)")
- // 是否为文件夹
- print(">>>> file isDirectory: \(file.isDirectory)")
- // 项目权限信息
- print(">>>> file filePermissions: \(file.filePermissions)")
- // 是否为指定路径的子项目
- print(">>>> file isChildOfPath: \(file.isChildOfPath(path))")
- }
- }
- // 添加一个方法,
- // 往一个已经存在的文本文件中, 写入新的内容.
- func writeFile()
- {
- // 添加一个异常捕捉语句, 用来执行写入文件的操作
- do
- {
- // 初始化一个字符串常量, 表示文件所在的沙箱路径.
- let file = NSHomeDirectory() + "/Documents/myFolder1/subFolder/firstFile_moved.txt"
- // 将字符串转换成路径对象
- let path = Path(file)
- // 将字符串写入到指定路径的文本文件中
- try "Here is CoolKeTang." |> TextFile(path: path)
- }
- catch
- {
- print("I can't write to a file?!")
- }
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- }
[Swift 通天遁地] 七, 数据与安全 -(6) 使用开源类库管理文件夹和操作文件
来源: http://www.bubuko.com/infodetail-2930469.html