处理文件 / 基本操作

let fileManager = FileManager.default

let dir = fileManager.urls(
    for: .documentDirectory,
    in: .userDomainMask).first!
let url = dir.appendingPathComponent("file.txt")

do {
    let attr = try fileManager.attributesOfItem(atPath: url.path)
    //file size (the first method)
    var fileSize = attr[FileAttributeKey.sizeasUInt64

    //file size (the second method)
    let dict = attr as NSDictionary
    fileSize = dict.fileSize()

    //file modification date
    let dateChanges = dict.fileModificationDate()

    //file creation date
    let creationDate = dict.fileCreationDate()

    //is readable file
    let isReadable = fileManager.isReadableFile(atPath: url.path)

    //is writable file
    let isWritable = fileManager.isWritableFile(atPath: url.path)

    //file extension
    let fileExtension = url.pathExtension

    //file name
    let fileName = url.deletingPathExtension().lastPathComponent

    //file directory
    let fileDir = url.deletingLastPathComponent().path

catch let e {
    print(e.localizedDescription)
}