1. //获取Document路径
  2. + (NSString *)getDocumentPath
  3. {
  4. NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  5. return [filePaths objectAtIndex:];
  6. }
  7.  
  8. //获取Library路径
  9. + (NSString *)getLibraryPath
  10. {
  11. NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  12. return [filePaths objectAtIndex:];
  13. }
  14.  
  15. //获取应用程序路径
  16. + (NSString *)getApplicationPath
  17. {
  18. return NSHomeDirectory();
  19. }
  20.  
  21. //获取Cache路径
  22. + (NSString *)getCachePath
  23. {
  24. NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  25. return [filePaths objectAtIndex:];
  26. }
  27.  
  28. //获取Temp路径
  29. + (NSString *)getTempPath
  30. {
  31. return NSTemporaryDirectory();
  32. }
  33.  
  34. //判断文件是否存在于某个路径中
  35. + (BOOL)fileIsExistOfPath:(NSString *)filePath
  36. {
  37. BOOL flag = NO;
  38. NSFileManager *fileManager = [NSFileManager defaultManager];
  39. if ([fileManager fileExistsAtPath:filePath]) {
  40. flag = YES;
  41. } else {
  42. flag = NO;
  43. }
  44. return flag;
  45. }
  46.  
  47. //从某个路径中移除文件
  48. + (BOOL)removeFileOfPath:(NSString *)filePath
  49. {
  50. BOOL flag = YES;
  51. NSFileManager *fileManage = [NSFileManager defaultManager];
  52. if ([fileManage fileExistsAtPath:filePath]) {
  53. if (![fileManage removeItemAtPath:filePath error:nil]) {
  54. flag = NO;
  55. }
  56. }
  57. return flag;
  58. }
  59.  
  60. //从URL路径中移除文件
  61. - (BOOL)removeFileOfURL:(NSURL *)fileURL
  62. {
  63. BOOL flag = YES;
  64. NSFileManager *fileManage = [NSFileManager defaultManager];
  65. if ([fileManage fileExistsAtPath:fileURL.path]) {
  66. if (![fileManage removeItemAtURL:fileURL error:nil]) {
  67. flag = NO;
  68. }
  69. }
  70. return flag;
  71. }
  72.  
  73. //创建文件路径
  74. +(BOOL)creatDirectoryWithPath:(NSString *)dirPath
  75. {
  76. BOOL ret = YES;
  77. BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:dirPath];
  78. if (!isExist) {
  79. NSError *error;
  80. BOOL isSuccess = [[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
  81. if (!isSuccess) {
  82. ret = NO;
  83. NSLog(@"creat Directory Failed. errorInfo:%@",error);
  84. }
  85. }
  86. return ret;
  87. }
  88.  
  89. //创建文件
  90. + (BOOL)creatFileWithPath:(NSString *)filePath
  91. {
  92. BOOL isSuccess = YES;
  93. NSFileManager *fileManager = [NSFileManager defaultManager];
  94. BOOL temp = [fileManager fileExistsAtPath:filePath];
  95. if (temp) {
  96. return YES;
  97. }
  98. NSError *error;
  99. //stringByDeletingLastPathComponent:删除最后一个路径节点
  100. NSString *dirPath = [filePath stringByDeletingLastPathComponent];
  101. isSuccess = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
  102. if (error) {
  103. NSLog(@"creat File Failed. errorInfo:%@",error);
  104. }
  105. if (!isSuccess) {
  106. return isSuccess;
  107. }
  108. isSuccess = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
  109. return isSuccess;
  110. }
  111.  
  112. //保存文件
  113. + (BOOL)saveFile:(NSString *)filePath withData:(NSData *)data
  114. {
  115. BOOL ret = YES;
  116. ret = [self creatFileWithPath:filePath];
  117. if (ret) {
  118. ret = [data writeToFile:filePath atomically:YES];
  119. if (!ret) {
  120. NSLog(@"%s Failed",__FUNCTION__);
  121. }
  122. } else {
  123. NSLog(@"%s Failed",__FUNCTION__);
  124. }
  125. return ret;
  126. }
  127.  
  128. //追加写文件
  129. + (BOOL)appendData:(NSData *)data withPath:(NSString *)path
  130. {
  131. BOOL result = [self creatFileWithPath:path];
  132. if (result) {
  133. NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:path];
  134. [handle seekToEndOfFile];
  135. [handle writeData:data];
  136. [handle synchronizeFile];
  137. [handle closeFile];
  138. return YES;
  139. } else {
  140. NSLog(@"%s Failed",__FUNCTION__);
  141. return NO;
  142. }
  143. }
  144.  
  145. //获取文件
  146. + (NSData *)getFileData:(NSString *)filePath
  147. {
  148. NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
  149. NSData *fileData = [handle readDataToEndOfFile];
  150. [handle closeFile];
  151. return fileData;
  152. }
  153.  
  154. //读取文件
  155. + (NSData *)getFileData:(NSString *)filePath startIndex:(long long)startIndex length:(NSInteger)length
  156. {
  157. NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
  158. [handle seekToFileOffset:startIndex];
  159. NSData *data = [handle readDataOfLength:length];
  160. [handle closeFile];
  161. return data;
  162. }
  163.  
  164. //移动文件
  165. + (BOOL)moveFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath
  166. {
  167. NSFileManager *fileManager = [NSFileManager defaultManager];
  168. if (![fileManager fileExistsAtPath:fromPath]) {
  169. NSLog(@"Error: fromPath Not Exist");
  170. return NO;
  171. }
  172. if (![fileManager fileExistsAtPath:toPath]) {
  173. NSLog(@"Error: toPath Not Exist");
  174. return NO;
  175. }
  176. NSString *headerComponent = [toPath stringByDeletingLastPathComponent];
  177. if ([self creatFileWithPath:headerComponent]) {
  178. return [fileManager moveItemAtPath:fromPath toPath:toPath error:nil];
  179. } else {
  180. return NO;
  181. }
  182. }
  183.  
  184. //拷贝文件
  185. +(BOOL)copyFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath
  186. {
  187. NSFileManager *fileManager = [NSFileManager defaultManager];
  188. if (![fileManager fileExistsAtPath:fromPath]) {
  189. NSLog(@"Error: fromPath Not Exist");
  190. return NO;
  191. }
  192. if (![fileManager fileExistsAtPath:toPath]) {
  193. NSLog(@"Error: toPath Not Exist");
  194. return NO;
  195. }
  196. NSString *headerComponent = [toPath stringByDeletingLastPathComponent];
  197. if ([self creatFileWithPath:headerComponent]) {
  198. return [fileManager copyItemAtPath:fromPath toPath:toPath error:nil];
  199. } else {
  200. return NO;
  201. }
  202. }
  203.  
  204. //获取文件夹下文件列表
  205. + (NSArray *)getFileListInFolderWithPath:(NSString *)path
  206. {
  207. NSFileManager *fileManager = [NSFileManager defaultManager];
  208. NSError *error;
  209. NSArray *fileList = [fileManager contentsOfDirectoryAtPath:path error:&error];
  210. if (error) {
  211. NSLog(@"getFileListInFolderWithPathFailed, errorInfo:%@",error);
  212. }
  213. return fileList;
  214. }
  215.  
  216. //获取文件大小
  217. + (long long)getFileSizeWithPath:(NSString *)path
  218. {
  219. unsigned long long fileLength = ;
  220. NSNumber *fileSize;
  221. NSFileManager *fileManager = [NSFileManager defaultManager];
  222. NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
  223. if ((fileSize = [fileAttributes objectForKey:NSFileSize])) {
  224. fileLength = [fileSize unsignedLongLongValue];
  225. }
  226. return fileLength;
  227.  
  228. // NSFileManager* manager =[NSFileManager defaultManager];
  229. // if ([manager fileExistsAtPath:path]){
  230. // return [[manager attributesOfItemAtPath:path error:nil] fileSize];
  231. // }
  232. // return 0;
  233. }
  234.  
  235. //获取文件创建时间
  236. + (NSString *)getFileCreatDateWithPath:(NSString *)path
  237. {
  238. NSString *date = nil;
  239. NSFileManager *fileManager = [NSFileManager defaultManager];
  240. NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
  241. date = [fileAttributes objectForKey:NSFileCreationDate];
  242. return date;
  243. }
  244.  
  245. //获取文件所有者
  246. + (NSString *)getFileOwnerWithPath:(NSString *)path
  247. {
  248. NSString *fileOwner = nil;
  249. NSFileManager *fileManager = [NSFileManager defaultManager];
  250. NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
  251. fileOwner = [fileAttributes objectForKey:NSFileOwnerAccountName];
  252. return fileOwner;
  253. }
  254.  
  255. //获取文件更改日期
  256. + (NSString *)getFileChangeDateWithPath:(NSString *)path
  257. {
  258. NSString *date = nil;
  259. NSFileManager *fileManager = [NSFileManager defaultManager];
  260. NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
  261. date = [fileAttributes objectForKey:NSFileModificationDate];
  262. return date;
  263. }

iOS中NSFileManager文件常用操作整合的更多相关文章

  1. Hadoop HDFS文件常用操作及注意事项

    Hadoop HDFS文件常用操作及注意事项 1.Copy a file from the local file system to HDFS The srcFile variable needs t ...

  2. go语言之进阶篇文件常用操作接口介绍和使用

    一.文件常用操作接口介绍 1.创建文件 法1: 推荐用法 func Create(name string) (file *File, err Error) 根据提供的文件名创建新的文件,返回一个文件对 ...

  3. python 异常处理、文件常用操作

    异常处理 http://www.jb51.net/article/95033.htm 文件常用操作 http://www.jb51.net/article/92946.htm

  4. OC NSFileManager(文件路径操作)

    OC NSFileManager(文件路径操作) 初始化 NSFileManager * fm = [NSFileManager defaultManager]; 获取当前目录 [fm current ...

  5. Python基础灬文件常用操作

    文件常用操作 文件内建函数和方法 open() :打开文件 read():输入 readline():输入一行 seek():文件内移动 write():输出 close():关闭文件 写文件writ ...

  6. iOS中几种常用的数据存储方式

    自己稍微总结了一下下,方便大家查看 1.write直接写入文件的方法 永久保存在磁盘中,可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据 ...

  7. java中 File文件常用操作方法的汇总

    一.IO流: 1.全称为:Input Output---------输入输出流. 输入:将文件读到内存中. 输出:将文件从内存中输出到其他地方. 2.IO技术的作用: 主要是解决设备与设备之间的数据传 ...

  8. linux下拷贝命令中的文件过滤操作记录

    在日常的运维工作中,经常会涉及到在拷贝某个目录时要排查其中的某些文件.废话不多说,下面对这一需求的操作做一记录: linux系统中,假设要想将目录A中的文件复制到目录B中,并且复制时过滤掉源目录A中的 ...

  9. [转]Windows系统中监控文件复制操作的几种方式

    1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得到源和目标文件名. 可以控制拒绝操作. 缺点: 不能对文件进行控制. 只对Shell文件操作有效, 对原生 ...

随机推荐

  1. 将SQLite移植到ARM板上 (转)

    SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它, 它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够 ...

  2. Eclipse与IDEA快捷键对比

    from:http://blog.csdn.net/dc_726/article/details/9531281 花了一天时间熟悉IDEA的各种操作,将各种快捷键都试了一下,感觉很是不错!于是就整理了 ...

  3. spring装载配置文件失败报错:org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException

    Tomcat容器启动失败,找到 debug日志一看: Context initialization failed org.springframework. beans.factory.xml.XmlB ...

  4. socket执行accept函数时没有进入阻塞状态,而是陷入了无限循环

    接着前两天继续看<VC深入详解>的网络编程部分,这次我快速看了遍书上的函数以及套接字C-S模型,然后自己从0开始写了个简单的服务端,结果发现一直在输出 而明明我还没有写客户端程序,由于打印 ...

  5. java代码----substring()方法是按索引截取字符串。。。下标0开始

    总结:按照索引substring(2,5);意思是从字符串的索引为2开始(包括)到第6个字符(不包括)的位置的截取部分 package com.s.x; //substring public clas ...

  6. Thread之六:线程创建方法

    1.继承Thread类,重写该类的run()方法. 2.实现Runnable接口,并重写该接口的run()方法,该run()方法同样是线程执行体,创建Runnable实现类的实例,并以此实例作为Thr ...

  7. [Java.Web][Servlet]常用请求头

    response.setStatus(302); response.setHeader("location", "/day04/1.html"); 这段代码可以 ...

  8. 【洛谷】P1196 银河英雄传说(并查集)

    题目描述 公元五八○一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶 ...

  9. CDH5.10 添加kafka服务

    简介: CDH的parcel包中是没有kafka的,kafka被剥离了出来,需要从新下载parcel包安装.或者在线安装,但是在线安装都很慢,这里使用下载parcel包离线安装的方式. PS:kafk ...

  10. Python Twisted系列教程18:Deferreds 全貌

    作者:dave@http://krondo.com/deferreds-en-masse/  译者: Cheng Luo 你可以从”第一部分 Twist理论基础“开始阅读:也可以从”Twisted 入 ...