在iOS应用开发的过程中,很多时候要用到数据存储,将数据存储在磁盘中。FileManager在存储的过程中就起到非常重要的作用,文件的管理都离不开它,这边文章从使用上简单讲一些常见用法。
写在前面:代码中的fileManeger,mainPath具体代码
private let fileManeger = FileManager.default
private let mainPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last ?? ""
一、新增文件/文件夹,以及数据写入
func createFolder(with folderName:String) -> Bool{
let folderPath = mainPath + folderName
if !fileManeger.fileExists(atPath: folderPath){
do {
try fileManeger.createDirectory(at: URL(fileURLWithPath: folderPath), withIntermediateDirectories: true, attributes: nil)
} catch {
print("创建失败!")
return false
}
}else{
print("已存在")
}
return true
}
func createNewFile(with filePath:String ,txt:String) -> Bool{
let txtfilePath = mainPath + filePath
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 recoveryFile(with filePath:String ,txt:String) -> Bool{
let txtfilePath = mainPath + filePath
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{
return fileManeger.createFile(atPath: filePath, contents: fileData, attributes: nil)
}
二、删除文件/文件夹
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
}
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
}
三、读取文件夹下目录,及文件数据
func loadFileList(with path:String) -> [String]?{
let folderPath = mainPath + path
if fileManeger.fileExists(atPath: folderPath){
return fileManeger.subpaths(atPath: folderPath)
}else{
return nil
}
}
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
}
四、复制或移动文件/文件夹
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("文件复制失败!")
}
}else{
print("文件路径不存在")
}
}
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("文件路径不存在")
}
}