在iOS應(yīng)用開發(fā)的過程中,很多時(shí)候要用到數(shù)據(jù)存儲,將數(shù)據(jù)存儲在磁盤中。FileManager在存儲的過程中就起到非常重要的作用含懊,文件的管理都離不開它身冬,這邊文章從使用上簡單講一些常見用法衅胀。
寫在前面:代碼中的fileManeger,mainPath具體代碼
private let fileManeger = FileManager.default
private let mainPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last ?? ""
一酥筝、新增文件/文件夾滚躯,以及數(shù)據(jù)寫入
//MARK: 新建文件夾
func createFolder(with folderName:String) -> Bool{
let folderPath = mainPath + folderName
if !fileManeger.fileExists(atPath: folderPath){
do {
//attributes:用來設(shè)置文件夾的一些屬性(只讀,讀寫等)
try fileManeger.createDirectory(at: URL(fileURLWithPath: folderPath), withIntermediateDirectories: true, attributes: nil)
} catch {
print("創(chuàng)建失敽俑琛掸掏!")
return false
}
}else{
print("已存在")
}
return true
}
//MARK: 新建文件
func createNewFile(with filePath:String ,txt:String) -> Bool{
let txtfilePath = mainPath + filePath
//這里以txt文本為例,也可寫入其他類型文件
let fileData = txt.data(using: String.Encoding.utf8)
if !fileManeger.fileExists(atPath: txtfilePath){
return self.writeDataToFilePath(with: txtfilePath, fileData: fileData)
}else{
print("已存在")
return false
}
}
//覆蓋數(shù)據(jù)宙帝,推薦使用replaceItemAt(originalItemURL: URL, withItemAt: URL, backupItemName: String, options: options)
//或replaceItemAt(originalItemURL: url, withItemAt: url)
func recoveryFile(with filePath:String ,txt:String) -> Bool{
let txtfilePath = mainPath + filePath
//這里以txt文本為例丧凤,也可寫入其他類型文件
let fileData = txt.data(using: String.Encoding.utf8)
if fileManeger.fileExists(atPath: txtfilePath){
return self.writeDataToFilePath(with: txtfilePath, fileData: fileData)
}else{
print("文件路徑不存在")
return false
}
}
func writeDataToFilePath(with filePath:String ,fileData:Data?) -> Bool{
//寫入數(shù)據(jù)也可以使用:fileData?.write(to: <#T##URL#>),attributes:用來設(shè)置文件的一些屬性(只讀步脓,讀寫等)
return fileManeger.createFile(atPath: filePath, contents: fileData, attributes: nil)
}
二愿待、刪除文件/文件夾
//MARK: 刪除文件夾
func removeFolder(with folderName:String) -> Bool{
let folderPath = mainPath + folderName
if fileManeger.fileExists(atPath: folderPath){
do {
try fileManeger.removeItem(atPath: folderPath)
} catch {
print("刪除失敽坡荨!")
return false
}
}else{
print("路徑不存在")
return false
}
return true
}
//MARK: 刪除文件
func removeFile(with filePath:String) -> Bool{
let txtfilePath = mainPath + filePath
if fileManeger.fileExists(atPath: txtfilePath){
do {
try fileManeger.removeItem(atPath: txtfilePath)
} catch {
print("刪除失斎越摹要出!")
return false
}
}else{
print("路徑不存在")
return false
}
return true
}
三、讀取文件夾下目錄农渊,及文件數(shù)據(jù)
//MARK:查看文件列表
func loadFileList(with path:String) -> [String]?{
let folderPath = mainPath + path
if fileManeger.fileExists(atPath: folderPath){
//subpathsOfDirectory(atPath: ) 可以查看完整詳細(xì)的子路徑 如:["test", "test/The only .txt"]
return fileManeger.subpaths(atPath: folderPath)
}else{
return nil
}
}
//MARK:加載文件內(nèi)容
func loadFileData(with path:String) ->Data?{
let filePath = mainPath + path
if fileManeger.fileExists(atPath: filePath){
do {
let fileData = try Data(contentsOf: URL(fileURLWithPath: filePath))
return fileData
} catch {
print("加載失敗")
return nil
}
}
print("路徑不存在")
return nil
}
四患蹂、復(fù)制或移動文件/文件夾
//復(fù)制和移動,是基于兩個文件路徑來操作的砸紊,所以兩個文件都需要存在传于。
//MARK: 復(fù)制文件
func copyFile(from oldPath:String,to newPath:String){
let oldFilePath = mainPath + oldPath
let newFilePath = mainPath + newPath
if fileManeger.fileExists(atPath: oldFilePath) && fileManeger.fileExists(atPath: newFilePath){
do {
try fileManeger.copyItem(atPath: oldFilePath, toPath: newFilePath)
} catch {
print("文件復(fù)制失敗醉顽!")
}
}else{
print("文件路徑不存在")
}
}
//MARK: 移動文件
func moveFile(from oldPath:String,to newPath:String){
let oldFilePath = mainPath + oldPath
let newFilePath = mainPath + newPath
if fileManeger.fileExists(atPath: oldFilePath) && fileManeger.fileExists(atPath: newFilePath){
do {
try fileManeger.moveItem(atPath: oldFilePath, toPath: newFilePath)
} catch {
print("文件移動失敻窳恕!")
}
}else{
print("文件路徑不存在")
}
}