FileManager 文件管理用法(Swift)

此处只列出了常用的一些功能

//
//  LKFileManger.swift
// 
//
//  Created by admin on 2017/5/22.
//  Copyright © 2017年 LK. All rights reserved.
//

import UIKit

class LKFileManger: NSObject {

    /// 单利
    static let sharedInstance: LKFileManger = LKFileManger()

    ///
    /// 获取Documents路径
    ///
    /// - Returns: 返回路径
    public func getDocumentsPath() -> String{

        let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)

        let path = paths.first

        print(path!)

        return path!
    }

    /// 根据传入的文件夹名创建文件夹📂
    ///
    /// - Parameter directoryName: 传入的文件夹名
    public func createDirectory(_ directoryName: String) {

        /// 获取路径
        let path = LKFileManger.sharedInstance.getDocumentsPath()

        /// 创建文件管理者
        let fileManger = FileManager.default

        /// 创建文件夹
        let directoryPath = path + ("/\(directoryName)")

        do {
            try fileManger.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
            print("文件夹创建成功")
        } catch let error  {
            print(error.localizedDescription)
            print("文件夹创建失败")
        }
    }

    /// 根据传入的文件名创建文件
    ///
    /// - Parameter fileName: 传入的文件名
    /// - Returns: 返回文件名
    public func createFile(_ fileName: String) -> (String){

        /// 获取路径
        let path = LKFileManger.sharedInstance.getDocumentsPath()

        /// 创建文件管理者
        let fileManger = FileManager.default

        /// 创建文件
        let filePath = path + ("/\(fileName)")

        if !fileManger.fileExists(atPath: filePath) { /// 先判断是否存在  不存在再创建

            let isSuccess = fileManger.createFile(atPath: filePath, contents: nil, attributes: nil)

            if isSuccess {
                print("文件创建成功")
            }else {
                print("文件创建失败")
            }
        }

        return filePath
    }

    /// 写入文件
    ///
    /// - Parameters:
    ///   - data: 要写入的数据
    ///   - filePath: 要写入的文件路径
    /// - Returns: 是否写入成功
    public func writeFile(_ data: AnyObject,  _ filePath: String) -> Bool {

        return  data.write(toFile: filePath, atomically: true)
    }

    /// 读取文件内容
    ///
    /// - Parameter filePath: 要读取的文件路径
    /// - Returns: 返回文件中数据
    public func readFileContent(_ filePath: String) -> AnyObject {

        /// 因为我的项目是存的数组 所以我返回的数组
        return NSArray(contentsOfFile: filePath) ?? []
    }



    /// 获取文件的大小
    ///
    /// - Returns: 文件大小
    public func getFileSize(_ fileName: String) -> Double {

        let fileManger = FileManager.default

        guard fileManger.fileExists(atPath: fileName) else {
            return 0
        }

        let attr = try! fileManger.attributesOfItem(atPath: fileName)

        let fileSize = Double((attr as NSDictionary).fileSize())

        return fileSize/1024/1024
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,216评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,206评论 19 139
  • 我本是世间俗物,有幸得你一瞥,从此化锦成铁。 我本以为可以与你比肩看这洪荒尽头,看这苍穹无迹,却未料到你眼中竟除了...
    沐森读书阅读 943评论 0 0
  • 人在社会中地位,身份,学历,收入等等可以有高有低,但是作为同一属性的个体-人,本质上都是一样的,这就需要我们互...
    颢宁斋阅读 8,061评论 37 122
  • 我时常会迷茫,这个明星到底是因为演技好火起来的,还是仅仅靠逗趣和头大吸了一批死忠粉。 他的身上有一种烟火气般的吸引...
    几多深与浅阅读 493评论 7 3