iOS开发——数据持久化Swift篇&(二)沙盒文件

沙盒文件

     //******************** 5.2 文件操作
     func use_FileOperations()
     {
         //1、获取程序的Home目录
         let homeDirectory = NSHomeDirectory()
         println(homeDirectory)

         //2、获取Documents目录
         let documentPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)

 //        let documentPath = documentPaths[0] as String

          //2015年5月2号修改
         let documentPath = documentPaths[] as! String
          println(documentPath)

         //或通过home目录自己追加文件夹
         var documentPath2 = homeDirectory + "/Documents"
         println(documentPath2)

         //3、获取Library目录
         let libraryPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)
 //        let libraryPath = libraryPaths[0] as String

         //2015年5月2号修改
         let libraryPath = libraryPaths[] as! String

         println(libraryPath)

         //或通过home目录自己追加文件夹
         var libraryPath2 = homeDirectory + "/Library"
         println(libraryPath2)

         //4、获取Cache目录
         let cachesPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
 //        let cachesPath = cachesPaths[0] as String

         //2015年5月2号修改
         let cachesPath = cachesPaths[] as! String
         println(cachesPath)

         //或通过home目录自己追加文件夹
         var cachesPath2 = homeDirectory + "/Library/Caches"
         println(cachesPath2)

         //5、获取Tmp目录
         let tmpDir = NSTemporaryDirectory()
         println(tmpDir)

         //或通过home目录自己追加文件夹
         var tmpDir2 = homeDirectory + "/tmp"
         println(tmpDir2)

         //对于错误信息
         var error:NSError?

         // 创建文件管理器
         var fileManager :NSFileManager = NSFileManager.defaultManager()

         //创建目录
         //定义几个自己的目录
         let myDirectory1:String = NSHomeDirectory() + "/Documents/myFolder/Images"
         let myDirectory2:String = NSHomeDirectory() + "/Documents/myFolder/Films"
         let myDirectory3:String = NSHomeDirectory() + "/Documents/myFolder/Musics"
         let myDirectory4:String = NSHomeDirectory() + "/Documents/myFolder/Files"

         //创建目录
         //withIntermediateDirectories 设置成true,代表中间所有的路径目录如果不存在,都会创建
         //如果设置成false,因为myFolder目录不存在,所以无法创建1234目录
         var isSuccess:Bool = fileManager.createDirectoryAtPath(myDirectory1, withIntermediateDirectories: true, attributes: nil, error: &error)
         println(error) //isSuccess=true代表创建成功,如果创建失败,error 会返回错误信息

         fileManager.createDirectoryAtPath(myDirectory2, withIntermediateDirectories: true, attributes: nil, error: &error)

         fileManager.createDirectoryAtPath(myDirectory3, withIntermediateDirectories: true, attributes: nil, error: &error)

         fileManager.createDirectoryAtPath(myDirectory4, withIntermediateDirectories: true, attributes: nil, error: &error)

         //创建文件
         var filePath = myDirectory1 + "/appInfo.txt"
         var info = "经常听到:被中介骗了,押金不退,晚一天交房租,被讹了。租房普遍现象:网上报价不真实?经常被忽悠!(看房时报价都比网上高!)证件不齐全,被骗过!(其实根本不是房东啦!)看房前态度都很热情! 签约之后态度骤变!入住后家电维修只能靠自己! 房屋到期,押金各种被勒索!现在开始,你来改变这一切!《租房点评》为你而备,租房无忧!再也不用担心被欺骗,想要知道给你介绍房子的人好不好,《租房点评》告诉你!"
         info.writeToFile(filePath, atomically: true, encoding: NSUTF8StringEncoding, error: &error)

         //保存图片
         var image = UIImage(named: "SwiftClassWeiXin@3x.png")
         var data:NSData = UIImageJPEGRepresentation(image, 1.0)
         var data1:NSData = UIImagePNGRepresentation(image)
         data.writeToFile(myDirectory1 + "/SwiftClassIcon.jpg", atomically: true)
         data1.writeToFile(myDirectory1 + "/SwiftClassIcon.png", atomically: true)

         //保存NSArray
         var array = NSArray(objects: ")
         array.writeToFile(myDirectory4 + "/array.plist", atomically: true)

         //保存NSDic
         var dictionary = NSDictionary(objects: ["])
         dictionary.writeToFile(myDirectory4 + "/dictionary.plist", atomically: true)

         //判断目录或文件是否存在
         var exist = fileManager.fileExistsAtPath(filePath)
         println(exist)

         //移动
         var filePath2 = myDirectory4 + "/appInfo.txt"
         fileManager.moveItemAtPath(filePath, toPath: filePath2, error: &error)

         //重命名
         //通过移动该文件对文件重命名
         var filePath3 = myDirectory4 + "/appInfo2.txt"
         fileManager.moveItemAtPath(filePath2, toPath: filePath3, error: &error)

         //拷贝
         var filePath4 = myDirectory4 + "/appInfoCopy.txt"
         fileManager.copyItemAtPath(filePath3, toPath: filePath4, error: &error)

         //获取目录列里所有文件名
         //先赋值一些文件到myDirectory4目录下
         var filePath00 = myDirectory4 + "/appInfoCopy00.txt"
         var filePath11 = myDirectory4 + "/appInfoCopy11.txt"
         var filePath22 = myDirectory4 + "/appInfoCopy22.txt"
         fileManager.copyItemAtPath(filePath3, toPath: filePath00, error: &error)
         fileManager.copyItemAtPath(filePath3, toPath: filePath11, error: &error)
         fileManager.copyItemAtPath(filePath3, toPath: filePath22, error: &error)

         //获取所有文件
         var fileArray = fileManager.subpathsAtPath(myDirectory4)
         println(fileArray)
 //        Optional([appInfo.txt, appInfo2.txt, appInfoCopy.txt, appInfoCopy00.txt, appInfoCopy11.txt, appInfoCopy22.txt])

         //IPhone中获取文件各项属性方法
         //获取文件属性
         var fileAttributes : [NSObject:AnyObject]? = NSFileManager.defaultManager().attributesOfItemAtPath(filePath22, error: &error)

         //获取文件的创建日期
         var modificationDate:AnyObject? = fileAttributes![NSFileModificationDate]
         println(modificationDate)

         //获取文件的字节大小
         var fileSize:AnyObject? = fileAttributes![NSFileSize]
         println(fileSize)

         //删除文件
         fileManager.removeItemAtPath(filePath4, error: &error)

         //删除目录下所有文件
         //方法1: 获取所有文件,然后遍历删除
         var fileArray2 : [AnyObject]? = fileManager.subpathsAtPath(myDirectory4)
         for fn in fileArray2!
         {
             fileManager.removeItemAtPath(myDirectory4 + "/\(fn)", error: &error)
         }

         //方法2:删除目录后重新创建该目录
         fileManager.removeItemAtPath(myDirectory4, error: &error)
         fileManager.createDirectoryAtPath(myDirectory4, withIntermediateDirectories: true, attributes: nil, error: &error)

     }
         //1:C函数
         var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)//第一个和第二个参数还有其他一些对应的选项,第三个参数就是~的表示(false-不展开~:...../User)
         var documentsDirectory = path[] as! String
         println(documentsDirectory)
         /**
         /Users/icocos/Library/Developer/CoreSimulator/Devices/BFD7850E-8B04-4710-B0CD-462FAB5F1035/data/Containers/Data/Application/B253FB89-3AA8-41C8-AE94-F820F856C06F/Documents
         */

         var temp = NSTemporaryDirectory()
         println(temp)
         /**
         /Users/icocos/Library/Developer/CoreSimulator/Devices/BFD7850E-8B04-4710-B0CD-462FAB5F1035/data/Containers/Data/Application/28CCDDA9-E804-4251-8BF9-EF68866F52C5/tmp/
         */

         //******************************

         //2:ios方法FileManager.这个使用更多,因为他还有很多的功能,就是我们下面的方法的使用
         var filemanager = NSFileManager.defaultManager()
         var documentDirectoryWithIOS = filemanager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
         println(documentDirectoryWithIOS)
         /**
         [file:///Users/icocos/Library/Developer/CoreSimulator/Devices/BFD7850E-8B04-4710-B0CD-462FAB5F1035/data/Containers/Data/Application/B253FB89-3AA8-41C8-AE94-F820F856C06F/Documents/]
         */
 
上一篇:IOS设备对position的支持性


下一篇:python 发送邮件模板