阳光博文 你的空间 知识的容器

IOS SWIFT 简单操作文件

//Home目录
let homeDirectory = NSHomeDirectory()
//Documents目录 苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包含此目录
let documentPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let documentPath = documentPaths[0] as! String
//let documentPath = homeDirectory + "/Documents"
        
//Library目录  这个目录下有2个子目录:Caches缓存 , Preferences偏好设置,不应该直接创建偏好设置文件而使用NSUserDefaults类
let libraryPaths =  NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let libraryPath = libraryPaths[0] as! String
//let libraryPath = homeDirectory + "/Library"
        
//Cache目录 主要存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出时删除
let cachesPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let cachesPath = cachesPaths[0] as! String
//let cachesPath = homeDirectory + "/Library/Caches"
        
//tmp目录  用于存放临时文件,保持应用程序再次启动过程中不需要的信息,重启后清空
let tmpDir = NSTemporaryDirectory()
//let tmpDir = homeDirectory + "/tmp"
        
//操作文件
var fileManager = NSFileManager.defaultManager()
var error : NSError?
        
let mydir1 = NSHomeDirectory() + "/Documents/myfolder/images"
let mydir2 = NSHomeDirectory() + "/Documents/myfolder/films"
let mydir3 = NSHomeDirectory() + "/Documents/myfolder/musics"
let mydir4 = NSHomeDirectory() + "/Documents/myfolder/files"
        
//withIntermediateDirectories 设置为true, 代表中间所有的路径目录如果不存在,都会创建
var isSuccess : Bool = fileManager.createDirectoryAtPath(mydir1, withIntermediateDirectories: true, attributes: nil, error: &error)
        
fileManager.createDirectoryAtPath(mydir2, withIntermediateDirectories: true, attributes: nil, error: &error)
fileManager.createDirectoryAtPath(mydir3, withIntermediateDirectories: true, attributes: nil, error: &error)
fileManager.createDirectoryAtPath(mydir4, withIntermediateDirectories: true, attributes: nil, error: &error)
        
//创建文件可以通过writeToFile方法将一些对象写入到文件中
var filePath = mydir4 + "/appInfo.txt"
var info  = "this is test text"
info.writeToFile(filePath, atomically: true, encoding: NSUTF8StringEncoding, error: &error)
        
//保存图片
var image = UIImage(named: "2.jpg")
var data : NSData = UIImageJPEGRepresentation(image, 1.0)
var data1 : NSData = UIImagePNGRepresentation(image)
data.writeToFile(mydir1 + "/2.jpg", atomically: true)
data1.writeToFile(mydir1 + "/2.png", atomically: true)
        
//保存NSAarry
var array = NSArray(objects: "111","222","333")
array.writeToFile(mydir4 + "/array.plist", atomically: true)
        
//保存NSDictionary
var dic = NSDictionary(objects: ["1111","2222","3333"], forKeys: ["1","2","3"])
dic.writeToFile(mydir4 + "/dictionary.plist", atomically: true)
        
//判断目录是否存在
var isExist = fileManager.fileExistsAtPath(filePath)
        
//移动文件
var filePath2 = mydir1 + "/appInfo.txt"
fileManager.moveItemAtPath(filePath, toPath: filePath2, error: &error)
        
//移动,重命名
var filePath3 = mydir1 + "/appInfo2.txt"
fileManager.moveItemAtPath(filePath2, toPath: filePath3, error: &error)
        
//拷贝,重命名
var filePath4 = mydir1 + "/appInfoCopy.txt"
fileManager.copyItemAtPath(filePath3, toPath: filePath4, error: &error)
        
//获取目录下所有文件
var fileArray = fileManager.subpathsAtPath(mydir1)
//println(fileArray)
        
//获取文件各项属性
var fileAttributes : [NSObject : AnyObject]? = NSFileManager.defaultManager().attributesOfItemAtPath(filePath2, error: &error)
var modificationDate = fileAttributes![NSFileModificationDate] as! String
println(modificationDate)  //2015-09-03 12:04:39 +0000
        
//删除文件
fileManager.removeItemAtPath(filePath4, error: &error)
        
//删除目录下所有文件
//方法1
var fileArray2 : [AnyObject]? = fileManager.subpathsAtPath(mydir1)
for f in fileArray2! {
  fileManager.removeItemAtPath(mydir1 + "/\(f)", error: &error)
}
//方法2
fileManager.removeItemAtPath(mydir1, error: &error)
fileManager.createDirectoryAtPath(mydir1, withIntermediateDirectories: true, attributes: nil, error: &error)



在线咨询