iOS FileManager使用

2,949 阅读2分钟
在iOS应用开发的过程中,很多时候要用到数据存储,将数据存储在磁盘中。FileManager在存储的过程中就起到非常重要的作用,文件的管理都离不开它,这边文章从使用上简单讲一些常见用法。
写在前面:代码中的fileManeger,mainPath具体代码
  private let fileManeger = FileManager.default
  private let mainPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last ?? ""

一、新增文件/文件夹,以及数据写入

    //MARK: 新建文件夹
    func createFolder(with folderName:String) -> Bool{
        
        let folderPath = mainPath + folderName
        if !fileManeger.fileExists(atPath: folderPath){
            do {
                //attributes:用来设置文件夹的一些属性(只读,读写等)
                try fileManeger.createDirectory(at: URL(fileURLWithPath: folderPath), withIntermediateDirectories: true, attributes: nil)
            } catch {
                print("创建失败!")
                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
        }
    }
    //覆盖数据,推荐使用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{
        //写入数据也可以使用:fileData?.write(to: <#T##URL#>),attributes:用来设置文件的一些属性(只读,读写等)
        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
    }

三、读取文件夹下目录,及文件数据

    //MARK:查看文件列表
    func loadFileList(with path:String) -> [String]?{
        let folderPath = mainPath + path
        if fileManeger.fileExists(atPath: folderPath){
            //subpathsOfDirectory(atPath: ) 可以查看完整详细的子路径 如:["test", "test/The only .txt"]
           return fileManeger.subpaths(atPath: folderPath)
        }else{
           return nil
        }
    }

    //MARK:加载文件内容
    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
    }

四、复制或移动文件/文件夹

    //复制和移动,是基于两个文件路径来操作的,所以两个文件都需要存在。
    //MARK: 复制文件
    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("文件路径不存在")
        }
        
    }
    //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("文件路径不存在")
        }
    }

demo地址:本地文件管理