基于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)
} }

Swift3 文件操作常用方法汇总的更多相关文章

  1. Joomla 文件操作常用方法

    今天介绍下joomla下文件操作常用方法,这些方法在文件读写,图片文件上传,等都有用处. jimport('joomla.filesystem.file'); $j = new JFile(); ge ...

  2. Java最全文件操作实例汇总

    本文实例汇总了Java文件操作.分享给大家供大家参考,具体如下: 1.创建文件夹 ? 1 2 3 4 5 6 7 8 9 10 11 //import java.io.*; File myFolder ...

  3. C# 文件操作常用方法总结

    需引用 System.IO Path为绝对路径 检测指定目录是否存在 Directory.Exists(Path) 创建目录 Directory.CreateDirectory(Path) 删除目录 ...

  4. C 语言文件操作

    C 语言文件操作 1. 数据流:     程序与数据的交互以流的形式进行.fopen 即打开数据流,fclose 即刷新数据流.     所谓数据流,是一种抽象,表示这段数据像流一样,需要逐步接收,不 ...

  5. python file文件操作--内置对象open

    说明: 1. 函数功能打开一个文件,返回一个文件读写对象,然后可以对文件进行相应读写操作. 2. file参数表示的需要打开文件的相对路径(当前工作目录)或者一个绝对路径,当传入路径不存在此文件会报错 ...

  6. DAY08、文件操作

    一.文件操作模式汇总: 主模式: r:读模式 w:写模式(无创建,有清空) a:追加(有创建的功能) x:写,必须自己创建文件,否则报错 从模式: t:文本操作(默认模式)r >rt,w> ...

  7. python基础-第四篇-4.2文件操作

    基本打开模式 文件操作的流程:打开文件,操作文件,关闭文件 打开文件方法:open(文件名,模式,编码) file = open(‘文件名’) 模式在不给值的情况下,默认为只读,而且如果是非当前的目录 ...

  8. [转]python file文件操作--内置对象open

    python file文件操作--内置对象open   说明: 1. 函数功能打开一个文件,返回一个文件读写对象,然后可以对文件进行相应读写操作. 2. file参数表示的需要打开文件的相对路径(当前 ...

  9. python03篇 字符串常用方法和文件操作(一)

    一.字符串常用方法 s1 = ' abcsfsfaadfdd ' s = s1.strip() print(s) print(len(s.strip())) print(s.count('a')) # ...

随机推荐

  1. maskrcnn_benchmark代码分析(2)

    maskrcnn_benchmark训练过程 ->训练命令: python tools/train_net.py --config-file "configs/e2e_mask_rcn ...

  2. Eclipse系列:如何设置Eclipse关联JDK源码和文档

    一.设置Eclipse关联JDK源码 1.打开Eclipse-->Windows-->Preferences       2. 在弹出的Preferences对话框中,Java--> ...

  3. kafka 主要内容介绍

    1.       kafka介绍 1.1.       主要功能 根据官网的介绍,ApacheKafka®是一个分布式流媒体平台,它主要有3种功能: 1:It lets you publish and ...

  4. 10款CSS3按钮 - 程序员再也不用为按钮设计而发愁了...

    这次主要给大家分享10款风格各异的CSS3按钮,如果你希望你的页面也能有很炫的样式,那么我相信这10款CSS3按钮就非常适合你,而且每一款都整理了源代码供参考,一起来看看吧. 1.绚丽的CSS3发光按 ...

  5. C# XmlTextWriter和XmlTextReader 读写XML文件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...

  6. C#.NET常见问题(FAQ)-如何使用变量动态添加控件

    可以先声明控件的数组   然后动态的添加到窗体中   在程序执行的时候你还可以随时通过数组下标访问到这些控件   更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youk ...

  7. 10分钟精通require.js

    require.js的诞生,就是为了解决这两个问题:(1)实现js文件的异步加载,避免网页失去响应:(2)管理模块之间的依赖性,便于代码的编写和维护. 实例下载:require.js应用实例 一.re ...

  8. vue重要特性

    重要特性 自定义input组件 动态组件 递归组件 slot 作用域slot 异步组件 内联模板 子组件索引 进阶 自定义指令 状态管理vuex 单文件组件 生产部署 路由 xxx

  9. 算法笔记_200:第三届蓝桥杯软件类决赛真题(C语言本科)

    目录 1 星期几 2 数据压缩 3 拼音字母 4 DNA比对 5 方块填数   前言:以下代码部分仅供参考,若有不当之处,还望路过同学指出哦~ 1 星期几 1949年的国庆节(10月1日)是星期六. ...

  10. time series review

    https://pdfs.semanticscholar.org/fc9c/1a94a15dd44c8c61b3e4841ecb9505f8cd37.pdf https://edoc.hu-berli ...