iOS开发——数据持久化Swift篇&(二)沙盒文件
沙盒文件
1 //******************** 5.2 文件操作 2 func use_FileOperations() 3 { 4 //1、获取程序的Home目录 5 let homeDirectory = NSHomeDirectory() 6 println(homeDirectory) 7 8 9 //2、获取Documents目录 10 let documentPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) 11 12 // let documentPath = documentPaths[0] as String 13 14 //2015年5月2号修改 15 let documentPath = documentPaths[0] as! String 16 println(documentPath) 17 18 //或通过home目录自己追加文件夹 19 var documentPath2 = homeDirectory + "/Documents" 20 println(documentPath2) 21 22 //3、获取Library目录 23 let libraryPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true) 24 // let libraryPath = libraryPaths[0] as String 25 26 //2015年5月2号修改 27 let libraryPath = libraryPaths[0] as! String 28 29 println(libraryPath) 30 31 //或通过home目录自己追加文件夹 32 var libraryPath2 = homeDirectory + "/Library" 33 println(libraryPath2) 34 35 36 37 38 //4、获取Cache目录 39 let cachesPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true) 40 // let cachesPath = cachesPaths[0] as String 41 42 //2015年5月2号修改 43 let cachesPath = cachesPaths[0] as! String 44 println(cachesPath) 45 46 //或通过home目录自己追加文件夹 47 var cachesPath2 = homeDirectory + "/Library/Caches" 48 println(cachesPath2) 49 50 51 //5、获取Tmp目录 52 let tmpDir = NSTemporaryDirectory() 53 println(tmpDir) 54 55 //或通过home目录自己追加文件夹 56 var tmpDir2 = homeDirectory + "/tmp" 57 println(tmpDir2) 58 59 60 //对于错误信息 61 var error:NSError? 62 63 // 创建文件管理器 64 var fileManager :NSFileManager = NSFileManager.defaultManager() 65 66 67 //创建目录 68 //定义几个自己的目录 69 let myDirectory1:String = NSHomeDirectory() + "/Documents/myFolder/Images" 70 let myDirectory2:String = NSHomeDirectory() + "/Documents/myFolder/Films" 71 let myDirectory3:String = NSHomeDirectory() + "/Documents/myFolder/Musics" 72 let myDirectory4:String = NSHomeDirectory() + "/Documents/myFolder/Files" 73 74 //创建目录 75 //withIntermediateDirectories 设置成true,代表中间所有的路径目录如果不存在,都会创建 76 //如果设置成false,因为myFolder目录不存在,所以无法创建1234目录 77 var isSuccess:Bool = fileManager.createDirectoryAtPath(myDirectory1, withIntermediateDirectories: true, attributes: nil, error: &error) 78 println(error) //isSuccess=true代表创建成功,如果创建失败,error 会返回错误信息 79 80 fileManager.createDirectoryAtPath(myDirectory2, withIntermediateDirectories: true, attributes: nil, error: &error) 81 82 fileManager.createDirectoryAtPath(myDirectory3, withIntermediateDirectories: true, attributes: nil, error: &error) 83 84 fileManager.createDirectoryAtPath(myDirectory4, withIntermediateDirectories: true, attributes: nil, error: &error) 85 86 87 88 //创建文件 89 var filePath = myDirectory1 + "/appInfo.txt" 90 var info = "经常听到:被中介骗了,押金不退,晚一天交房租,被讹了。租房普遍现象:网上报价不真实?经常被忽悠!(看房时报价都比网上高!)证件不齐全,被骗过!(其实根本不是房东啦!)看房前态度都很热情! 签约之后态度骤变!入住后家电维修只能靠自己! 房屋到期,押金各种被勒索!现在开始,你来改变这一切!《租房点评》为你而备,租房无忧!再也不用担心被欺骗,想要知道给你介绍房子的人好不好,《租房点评》告诉你!" 91 info.writeToFile(filePath, atomically: true, encoding: NSUTF8StringEncoding, error: &error) 92 93 //保存图片 94 var image = UIImage(named: "[email protected]") 95 var data:NSData = UIImageJPEGRepresentation(image, 1.0) 96 var data1:NSData = UIImagePNGRepresentation(image) 97 data.writeToFile(myDirectory1 + "/SwiftClassIcon.jpg", atomically: true) 98 data1.writeToFile(myDirectory1 + "/SwiftClassIcon.png", atomically: true) 99 100 //保存NSArray 101 var array = NSArray(objects: "111","222","333") 102 array.writeToFile(myDirectory4 + "/array.plist", atomically: true) 103 104 //保存NSDic 105 var dictionary = NSDictionary(objects: ["1111","22222","33333"], forKeys: ["1","2","3"]) 106 dictionary.writeToFile(myDirectory4 + "/dictionary.plist", atomically: true) 107 108 109 //判断目录或文件是否存在 110 var exist = fileManager.fileExistsAtPath(filePath) 111 println(exist) 112 113 //移动 114 var filePath2 = myDirectory4 + "/appInfo.txt" 115 fileManager.moveItemAtPath(filePath, toPath: filePath2, error: &error) 116 117 //重命名 118 //通过移动该文件对文件重命名 119 var filePath3 = myDirectory4 + "/appInfo2.txt" 120 fileManager.moveItemAtPath(filePath2, toPath: filePath3, error: &error) 121 122 //拷贝 123 var filePath4 = myDirectory4 + "/appInfoCopy.txt" 124 fileManager.copyItemAtPath(filePath3, toPath: filePath4, error: &error) 125 126 127 //获取目录列里所有文件名 128 //先赋值一些文件到myDirectory4目录下 129 var filePath00 = myDirectory4 + "/appInfoCopy00.txt" 130 var filePath11 = myDirectory4 + "/appInfoCopy11.txt" 131 var filePath22 = myDirectory4 + "/appInfoCopy22.txt" 132 fileManager.copyItemAtPath(filePath3, toPath: filePath00, error: &error) 133 fileManager.copyItemAtPath(filePath3, toPath: filePath11, error: &error) 134 fileManager.copyItemAtPath(filePath3, toPath: filePath22, error: &error) 135 136 //获取所有文件 137 var fileArray = fileManager.subpathsAtPath(myDirectory4) 138 println(fileArray) 139 // Optional([appInfo.txt, appInfo2.txt, appInfoCopy.txt, appInfoCopy00.txt, appInfoCopy11.txt, appInfoCopy22.txt]) 140 141 142 //IPhone中获取文件各项属性方法 143 //获取文件属性 144 var fileAttributes : [NSObject:AnyObject]? = NSFileManager.defaultManager().attributesOfItemAtPath(filePath22, error: &error) 145 146 //获取文件的创建日期 147 var modificationDate:AnyObject? = fileAttributes![NSFileModificationDate] 148 println(modificationDate) 149 150 151 //获取文件的字节大小 152 var fileSize:AnyObject? = fileAttributes![NSFileSize] 153 println(fileSize) 154 155 156 //删除文件 157 fileManager.removeItemAtPath(filePath4, error: &error) 158 159 //删除目录下所有文件 160 //方法1: 获取所有文件,然后遍历删除 161 var fileArray2 : [AnyObject]? = fileManager.subpathsAtPath(myDirectory4) 162 for fn in fileArray2! 163 { 164 fileManager.removeItemAtPath(myDirectory4 + "/\(fn)", error: &error) 165 } 166 167 //方法2:删除目录后重新创建该目录 168 fileManager.removeItemAtPath(myDirectory4, error: &error) 169 fileManager.createDirectoryAtPath(myDirectory4, withIntermediateDirectories: true, attributes: nil, error: &error) 170 171 172 173 174 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。