本文提供獲取路徑的兩種方法界弧,返回的類型都是String
插爹,還有一種獲取路徑的方法返回的是URL
扫步,FileManager
也提供了通過(guò)UR
L操作文件的相關(guān)方法俭茧,和通過(guò)pathString
操作的方法基本是一一對(duì)應(yīng),感興趣的同學(xué)可以對(duì)下面的代碼進(jìn)行替換碌尔,使用URL
作為路徑的返回類型和FileManager函數(shù)的參數(shù)操作相關(guān)文件捏萍。
1.常用獲取路徑方法
enum AppDirectories {
case documents
case library
case libraryCaches
case temp
case customPath(path: String)
}
// 本文提供了兩種獲取路徑的方法系冗,返回類型是String绢记,還有一種獲取路徑的方法返回的是URL扁达,F(xiàn)ileManager也提供了通過(guò)URL操作文件的相關(guān)方法,和通過(guò)pathString操作的方法基本是一一對(duì)應(yīng)庭惜,感興趣的同學(xué)可以對(duì)下面的代碼進(jìn)行替換罩驻,使用URL作為路徑的返回類型和函數(shù)的參數(shù)操作文件穗酥。
/// MARK: - 獲取路徑 以下兩種方法都可以
struct FilePathUtils {
//document
static func documentsDirectoryPath() -> String {
// return NSSearchPathForDirectoriesInDomains(.userDirectory, .userDomainMask, true).first!
return NSHomeDirectory().appending("/Documents")
}
//library
static func libraryDirectoryPath() -> String {
// return NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first!
return NSHomeDirectory().appending("/Library")
}
//temp
static func tempDirectoryPath() -> String {
// return NSTemporaryDirectory()
return NSHomeDirectory().appending("/tmp")
}
//Library/Caches
static func librayCachesPath() -> String {
// return NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
return NSHomeDirectory().appending("/Library/Caches")
}
//根據(jù)枚舉值返回不同的url
static func setupFilePath(directory: AppDirectories, name: String) -> String {
return getPath(for: directory) + name
}
private static func getPath(for directory: AppDirectories) -> String {
switch directory {
case .documents:
return documentsDirectoryPath()
case .libraryCaches:
return librayCachesPath()
case .library:
return libraryDirectoryPath()
case .temp:
return tempDirectoryPath()
case .customPath(let path):
return path
}
}
}
2.文件操作的相關(guān)方法
/// MARK: - 文件操作
struct FileUtils {
// MARK: - 創(chuàng)建文件
static func createFolder(basePath: AppDirectories, folderName: String, createIntermediates: Bool = true, attributes: [FileAttributeKey: Any]? = nil) -> Bool {
let filePath = FilePathUtils.setupFilePath(directory: basePath, name: folderName)
let fileManager = FileManager.default
do {
try fileManager.createDirectory(atPath: filePath, withIntermediateDirectories: createIntermediates, attributes: attributes)
return true
} catch {
return false
}
}
// MARK: - 寫入文件
// options: 默認(rèn)先創(chuàng)建一個(gè)臨時(shí)文件护赊,直到文件內(nèi)容寫入成功再導(dǎo)入到目標(biāo)文件里。 如果為NO砾跃,則直接寫入目標(biāo)文件里骏啰。
static func writeFile(content: Data, filePath: String, options: Data.WritingOptions = []) -> Bool {
do {
try content.write(to: URL(string: filePath)!, options: options)
return true
} catch {
return false
}
}
// MARK: - 讀取文件
static func readFile(filePath: String) -> Data? {
let fileContents = FileManager.default.contents(atPath: filePath)
if fileContents?.isEmpty == false {
return fileContents
} else {
return nil
}
}
// MARK: - 刪除文件
static func deleteFile(filePath: String) -> Bool {
do {
try FileManager.default.removeItem(atPath: filePath)
return true
} catch {
return false
}
}
// MARK: - 重命名文件
static func renameFile(path: AppDirectories, oldName: String, newName: String) -> Bool {
let oldPath = FilePathUtils.setupFilePath(directory: path, name: oldName)
let newPath = FilePathUtils.setupFilePath(directory: path, name: newName)
do {
try FileManager.default.moveItem(atPath: oldPath, toPath: newPath)
return true
} catch {
return false
}
}
// MARK: - 移動(dòng)文件
static func moveFile(fileName: String, fromDirectory: String, toDirectory: String) -> Bool {
let originPath = FilePathUtils.setupFilePath(directory: .customPath(path: fromDirectory), name: fileName)
let destinationPath = FilePathUtils.setupFilePath(directory: .customPath(path: toDirectory), name: fileName)
do {
try FileManager.default.moveItem(atPath: originPath, toPath: destinationPath)
return true
} catch {
return false
}
}
// MARK: - 拷貝文件
static func copyFile(fileName: String, fromDirectory: String, toDirectory: String) throws {
let originPath = FilePathUtils.setupFilePath(directory: .customPath(path: fromDirectory), name: fileName)
let destinationPath = FilePathUtils.setupFilePath(directory: .customPath(path: toDirectory), name: fileName)
return try FileManager.default.copyItem(atPath: originPath, toPath: destinationPath)
}
// MARK: - 文件是否可寫
static func isWritable(filePath: String) -> Bool {
if FileManager.default.isWritableFile(atPath: filePath) {
return true
} else {
return false
}
}
// MARK: - 文件是否可讀
static func isReadable(filePath: String) -> Bool {
if FileManager.default.isReadableFile(atPath: filePath) {
return true
} else {
return false
}
}
// MARK: - 文件是否存在
static func exists(filePath: String) -> Bool {
if FileManager.default.fileExists(atPath: filePath) {
return true
} else {
return false
}
}
// MARK: - 獲取文件列表
static func getFilePathList(folderPath: String) -> [String] {
let fileManager = FileManager.default
let fileList = try? fileManager.contentsOfDirectory(atPath: folderPath)
return fileList ?? []
}
}