@interface NSFileManager : NSObject

NSFileManager操作:

1.文件夹创建删除

2.文件创建删除

3.清除数据缓存

以上办法都在这里:https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW1

笔记:

NSFileManager的文件对象操作的优势:

 NSFileManager. An advantage of file reference URLs is that they are less fragile than path-based URLs while your app is running. If the user moves a file in the Finder, any path-based URLs that refer to the file immediately become invalid and must be updated to the new path. However, as long as the file moved to another location on the same disk, its unique ID does not change and any file reference URLs remain valid.

1>sandbox中的执意要两大部分:bundle container and  data container ,etc.,如图:

2>主要记录Data container

2.1>Documents/ 可以创建保存文件和文件夹,目的很明确,让用户创建和操作的文件(暴漏的)。文件的内容可以在iTunces备份。

2.2>Library/ 跟Documents文件差不多,但是它里面在创建之初就已经创建几个默认的文件夹了,eg:Caches/  Preferences/ etc.(Preference/ 中放着NSUserDefault关联的plist文件)

Caches/ 中存在SnapShot:app截图 。

2.3>Library/Application support/ 文件,一般是用户区创建也可以操作,但是是hidden(不暴漏的),用于存储配置文件,模板等app系统配置相关的文件。

3>Accessing Files and Directories(文件路径操作读写)

Before you can open a file, you first have to locate it in the file system. The system frameworks provide many routines for obtaining references to many well-known directories, such as the Library directory and its contents. You can also specify locations manually by building a URL or string-based path from known directory names.

When you know the location of a file, you can then start planning the best way to access it. Depending on the type of file, you may have several options. For known file types, you typically use built-in system routines to read or write the file contents and give you an object that you can use. For custom file types, you may need to read the raw file data yourself.

Choose the Right Way to Accessing Files

Although you can open any file and read its contents - lists the common file types supported by the system along with information about how you access them.

读到这里,大体上说拼接一个Location来获取文件,有两种方法:NSURL和NSString

但是,官方文档说了,如果你想获取一个本地的地址,最好使用NSURL来获取本地地址Access文件,文档中说啦:

The preferred way to specify the location of a file or directory is to use the NSURL class. Although the NSString class has many methods related to path creation, URLs offer a more robust(牛逼的) way to locate files and directories. For apps that also work with network resources, URLs also mean that you can use one type of object to manage items located on a local file system or on a network server

建议用NSURL拼接本地地址

当然,官方文档又说了,还有一个东西跟NSURL功能可以互换的,叫做CGURLRef

Note: In addition to NSURL, you can also use the CFURLRef opaque type to manipulate paths as URLs. The NSURL class is toll-free bridged with the CFURLRef type, which means you can use them interchangeably in your code. For more information about how to create and manipulate URLs using Core Foundation, see CFURL Reference.

CGURLRef官方也推荐

使用NSURL来创建文件夹:

    NSFileManager * shareFM = [NSFileManager defaultManager];
    NSArray * possibleURLs = [shareFM URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
    NSURL * appSupportDir = nil;
    NSURL * appDirectory = nil;

    )
    {
        appSupportDir = [possibleURLs objectAtIndex:];
    }
    if (appSupportDir)
    {
        NSString * appBundleID = [[NSBundle mainBundle] bundleIdentifier];
        appDirectory = [appSupportDir URLByAppendingPathComponent:appBundleID];
        [shareFM createDirectoryAtURL:appDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    }

    NSLog(@"%@",appDirectory);

NSURL创建文件夹

4.Enumerating the Contents of a Directory // 遍历文件夹中文件

Enumerating a directory one file at a time is recommended when you want to search for a specific file and stop enumerating when you find it. File-by-file enumeration uses the NSDirectoryEnumerator class, which defines the methods for retrieving items. Because NSDirectoryEnumerator itself is an abstract class, however, you do not create instances of it directly. Instead, you use either the enumeratorAtURL:includingPropertiesForKeys:options:errorHandler: or the enumeratorAtPath: method of NSFileManager object to obtain a concrete instance of the class for use in your enumeration.

遍历文件

5.Use Secure Coding Practices(使用安全编码规范)

5.1>删除一个空文件的时候,要求之前判断文件是空的时候,在执行删除。

5.2>在进行文件操作的时候权限不是执行操作独立的,所以在你创建一个文件之前判断她是不存在的,或者你必须肯定读,写数据的文件是你创建的。

5.3>无论任何时候,操作文件使用名称好过文件路径,这样的方式可以保证你一直操作同一个文件。

5.4>把创建文件的方法独立成一个方法,这样你可以更好的知道你所创建的文件是不是已经一个已经存在的。

5.5>读一个文件的时候,首先判断是否有是自己权限和你要操作的权限,这样可以避免程序hanging,在失败的时候可以进行很好的失败处理。

@interface NSFileManager : NSObject的更多相关文章

  1. iOS - OC NSFileManager 文件管理

    前言 @interface NSFileManager : NSObject @interface NSFileHandle : NSObject <NSSecureCoding> NSF ...

  2. 用NSData和NSFileManager保存内存中的对象

    曾经接触过iOS开发,并且开发过两个应用,纵然青涩,也算是一断美好的回忆.转眼就已经一年多了!现在回过头来决定再次拿起iOS开发. 下面讲NSData: NSdata的概念 1.使用文件时需要频繁地将 ...

  3. 【OC加强】辛格尔顿和[NSFileManager defaultMagager]以及其他设计模式

    我们在工作中使用文件NSFileManager上课时间,创建发现1对象,此2同样的对象地址: NSFileManager *file1=[NSFileManager defaultManager]; ...

  4. 数据持久化-存取方式总结&应用沙盒&文件管理NSFileManager

    iOS应用数据存储的常用方式:  1.XML属性列表   (plist归档)  2.NSUserDefaults (偏好设置)  3.NSKeyedArchiver  归档(加密形式)  4.SQLi ...

  5. YYModel 源码解读(二)之NSObject+YYModel.h (2)

    _YYModelMeta   这个内部的类主要是对这个类的描述.包含了和此类转换相关的数据. /// A class info in object model. @interface _YYModel ...

  6. YYModel 源码解读(二)之NSObject+YYModel.h (1)

    本篇文章主要介绍 _YYModelPropertyMeta 前边的内容 首先先解释一下前边的辅助函数和枚举变量,在写一个功能的时候,这些辅助的东西可能不是一开始就能想出来的,应该是在后续的编码过程中 ...

  7. iOS Class 使用NSProxy和NSObject设计代理类的差异

    经常发现在一些需要使用消息转发而创建代理类时, 不同的程序员都有着不同的使用方法, 有些采用继承于NSObject, 而有一些采用继承自NSProxy. 二者都是Foundation框架中的基类, 并 ...

  8. 只用@property定义一个属性speed,子类不能直接用_speed,需要在interface的成员变量列表里写上_speed

    //写法一: @interface Person : NSObject { } @property (nonatomic, strong) NSString *name; @end @implemen ...

  9. IOS OC声明变量在@interface括号中与使用@property的区别

    方式一:直接在@interface中的大括号中声明. @interface MyTest : NSObject{ NSString *mystr; } 方式二:在@interface中声明,然后再在@ ...

随机推荐

  1. 怎样修改 Openstack Horizon(Dashboard)的显示界面 (一)

    Openstack 有很多项目,比如 nova 是虚拟机管理,neutron 是虚拟网络管理, glance 是存储管理,而 horizon 是负责 Openstack 的统一界面.horizon 的 ...

  2. Python 处理数据库返回结果

    游标执行后返回的结果都只是数据,但是不带有列名标识.这里需要处理2个问题: 将返回的数据映射到每一列上 当返回的结果很大的时候,需要使用迭代器来提升性能. 解决上面的2个问题,在python里面可以采 ...

  3. 基于FPGA的电压表与串口通信(上)

    实验原理 该实验主要为利用TLC549采集模拟信号,然后将模拟信号的数字量通过串口发送到PC上上位机进行显示,使用到的TLC549驱动模块在进阶实验已经使用到了,串口模块在基础实验也已经使用到了,本实 ...

  4. [BZOJ1263][SCOI2006]整数划分(数学+高精度)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1263 分析:数学老师上课讲过啦= =,就是尽可能3越多越好.然后就写个高精度就行了.

  5. Win2008上.NET4.0部署出错HTTP 错误 500.21 - Internal Server Error的解决方法

    原因:在安装Framework v4.0之后,再启用IIS,导致Framework没有完全安装 解决:开始->所有程序->附件->鼠标右键点击“命令提示符”->以管理员身份运行 ...

  6. C# has three timers

    结论 *1.窗体timer和线程timer.计时器timer不同,因为后两者dispose之后,GC可以收集,而前者无法收集 *2.如果一个对象的成员函数正在被执行,那么这个对象肯定不会被收集 *3. ...

  7. 东大OJ-Max Area

    1034: Max Area 时间限制: 1 Sec  内存限制: 128 MB 提交: 40  解决: 6 [提交][状态][讨论版] 题目描述 又是这道题,请不要惊讶,也许你已经见过了,那就请你再 ...

  8. linux 下第一个cordova android app

    上篇博客写了linux下 cordova + ionic 环境的搭建 , 今天就来做下第一个app的简单讲解吧 首先昨天已经可以通过命令行的方式创建app了.经过今天好一段时间的研究发现使用 ioni ...

  9. 【CodeVS 1199】【NOIP 2012】开车旅行

    http://codevs.cn/problem/1199/ 主要思想是倍增,对于第一个回答从后往前扫,依次插入平衡树中. 我写的splay,比较繁琐. #include<cmath> # ...

  10. bootstarp风格的toggle效果分享

    最近在写项目的时候想要一个这样的效果: 我知道这个效果在 flat-ui中有, 但是我又不想引用一整个flat-ui; 这个效果依赖html5的transition, 所以浏览器兼容成问题: 从fla ...