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