Swift3 文件操作常用方法匯總


基於swift3的一些常用文件操作方法:

1、創建文件(文件夾)

2、讀取文件(根據名稱)

3、讀取文件(根據路徑)

4、判斷文件是否存在(根據名稱)

5、判斷文件是否存在(根據路徑)

6、刪除指定名稱文件

7、刪除指定路徑文件

8、刪除所有文件

9、寫入文件

 

代碼:

//第一層文件夾名稱
let CACHEPATH = "K12Cache"

//內層文件夾名稱
let MarkingPath = "K12Cache/Marking/"
class CacheUtils{

    //緩存路徑(這里用的沙盒cache文件,非document,可根據需求更改)
    class func getCachePath()->String{
        var cacheDir = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, .userDomainMask, true).first!
        if(!cacheDir.hasSuffix("/")){
            cacheDir += "/"
        }
        cacheDir += CACHEPATH + "/"
        return cacheDir
    }
    
    //獲得NSFileManager
    class func getFileManager()->FileManager{
        return FileManager.default
    }
    
    //判斷文件夾是否存在
    class func dirExists(dir:String)->Bool{
        return getFileManager().fileExists(atPath: dir)
    }
    
    //判斷文件是否存在
    class func fileExists(path:String)->Bool{
        return dirExists(dir: path)
    }
    
    //判斷是否存在,存在則返回文件路徑,不存在則返回nil
    class func fileExistsWithFileName(fileName:String)->String?{
        let dir = getCachePath()
        if(!dirExists(dir: dir)){
            return nil
        }
        let filePath = dir + fileName
        
        return fileExists(path: filePath) ? filePath : nil
    }
    
    
    //創建文件夾
    class func createDir(dir:String)->Bool{
        let fileManager = getFileManager()
        do{
            try fileManager.createDirectory(at: NSURL(fileURLWithPath: dir, isDirectory: true) as URL, withIntermediateDirectories: true, attributes: nil)
        }catch{
            return false
        }
        return true
    }
    
    /// 根據文件名創建路徑
    ///
    /// - Parameter fileName: <#fileName description#>
    /// - Returns: <#return value description#>
    class func createFilePath(fileName:String)->String?{
        let dir = getCachePath()
        if(!dirExists(dir: dir) && !createDir(dir: dir)){
            return nil
        }
        let filePath = dir + fileName
        if(fileExists(path: filePath)){
            do{
                try getFileManager().removeItem(atPath: filePath)
            }catch{
                return nil
            }
            
        }
        return filePath
    }
    
    
    /// 刪除文件 - 根據文件名稱
    ///
    /// - Parameter fileName: <#fileName description#>
    /// - Returns: <#return value description#>
    class func deleteFileWithName(fileName:String)->Bool{
        guard let filePath = fileExistsWithFileName(fileName: fileName) else{
            return true
        }
        return deleteFile(path: filePath)
    }
    
    
    /// 刪除文件 - 根據文件路徑
    ///
    /// - Parameter path: <#path description#>
    /// - Returns: <#return value description#>
    class func deleteFile(path:String)->Bool{
        if(!fileExists(path: path)){
            return true
        }
        let fileManager = getFileManager()
        do{
            try fileManager.removeItem(atPath: path)
        }catch{
            return false
        }
        
        return true
    }
    
    /**
     清除所有的緩存
     
     - returns: Bool
     */
    class func deleteAll()->Bool{
        let dir = getCachePath()
        
        if !dirExists(dir: dir){
            return true
        }
        let manager = getFileManager()
        do{
            try manager.removeItem(atPath: dir)
        }catch{
            return false
        }
        return true
    }
    
    //讀取文件 -(根據路徑)
    class func readFileFromCache(path:String)->NSData?{
        var result:NSData?
        do{
            result = try NSData(contentsOfFile: path, options: NSData.ReadingOptions.uncached)
        }catch{
            return nil
        }
        return result
    }
    
    //讀取文件 -(根據文件名)
    class func readFile(fileName:String)->NSData?{
        
        guard let filePath = fileExistsWithFileName(fileName: fileName) else{
            return nil
        }
        
        return readFileFromCache(path: filePath)
    }
    

    
    /// 寫文件
    ///
    /// - Parameters:
    ///   - fileName: 文件名稱
    ///   - data: 數據data
    /// - Returns: <#return value description#>
    class func writeFile(fileName:String,data:NSData)->Bool{
        
        guard let filePath = createFilePath(fileName: fileName) else{
            return false
        }
        
        return data.write(toFile: filePath, atomically: true)
    }
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM