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)
}
}
Swift3 文件操作常用方法汇总的更多相关文章
- Joomla 文件操作常用方法
今天介绍下joomla下文件操作常用方法,这些方法在文件读写,图片文件上传,等都有用处. jimport('joomla.filesystem.file'); $j = new JFile(); ge ...
- Java最全文件操作实例汇总
本文实例汇总了Java文件操作.分享给大家供大家参考,具体如下: 1.创建文件夹 ? 1 2 3 4 5 6 7 8 9 10 11 //import java.io.*; File myFolder ...
- C# 文件操作常用方法总结
需引用 System.IO Path为绝对路径 检测指定目录是否存在 Directory.Exists(Path) 创建目录 Directory.CreateDirectory(Path) 删除目录 ...
- C 语言文件操作
C 语言文件操作 1. 数据流: 程序与数据的交互以流的形式进行.fopen 即打开数据流,fclose 即刷新数据流. 所谓数据流,是一种抽象,表示这段数据像流一样,需要逐步接收,不 ...
- python file文件操作--内置对象open
说明: 1. 函数功能打开一个文件,返回一个文件读写对象,然后可以对文件进行相应读写操作. 2. file参数表示的需要打开文件的相对路径(当前工作目录)或者一个绝对路径,当传入路径不存在此文件会报错 ...
- DAY08、文件操作
一.文件操作模式汇总: 主模式: r:读模式 w:写模式(无创建,有清空) a:追加(有创建的功能) x:写,必须自己创建文件,否则报错 从模式: t:文本操作(默认模式)r >rt,w> ...
- python基础-第四篇-4.2文件操作
基本打开模式 文件操作的流程:打开文件,操作文件,关闭文件 打开文件方法:open(文件名,模式,编码) file = open(‘文件名’) 模式在不给值的情况下,默认为只读,而且如果是非当前的目录 ...
- [转]python file文件操作--内置对象open
python file文件操作--内置对象open 说明: 1. 函数功能打开一个文件,返回一个文件读写对象,然后可以对文件进行相应读写操作. 2. file参数表示的需要打开文件的相对路径(当前 ...
- python03篇 字符串常用方法和文件操作(一)
一.字符串常用方法 s1 = ' abcsfsfaadfdd ' s = s1.strip() print(s) print(len(s.strip())) print(s.count('a')) # ...
随机推荐
- Set Matrix Zeroes leetcode java
题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cl ...
- 小米推送 简介 集成 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Oracle数据库将varchar类型的字段改为clob类型
alter table pwlp_accuse_info modify INSTRUCTION_STYLE long; alter table pwlp_accuse_info modify INST ...
- layer.tips定义弹出的宽度
layer.tips('xxx', '.onlinetest', { tips: [1, '#3595CC'], area: ['500px', 'auto'], time: 4000 });
- python 爬虫随机获取User-Agent
可以有两种方法: 1.随机生成 首先安装 pip install fake-useragent import random from fake_useragent import UserAgent d ...
- Redis 在线管理工具(phpRedisAdmin)介绍
phpRedisAdmin is a simple web interface to manage Redis databases. phpRedisAdmin 在 Redis clients 的列表 ...
- Ext4中获取下拉框的值
var supplierCombo = Ext.getCmp("rkSupplierCombo_id"); var supplierId = supplierCombo.getV ...
- JPA(七):映射关联关系------映射双向多对一的关联关系
映射双向多对一的关联关系 修改Customer.java package com.dx.jpa.singlemanytoone; import java.util.Date; import java. ...
- 转: linux进程地址图解
http://www.cnblogs.com/clover-toeic/p/3754433.html
- ACM~排列组合&&hdu例子
排列组合是数学中的一个分支.在计算机编程方面也有非常多的应用,主要有排列公式和组合公式.错排公式.母函数.Catalan Number(卡特兰数)等. 一.有关组合数学的公式 1.排列公式 P(n ...