NSFileManager用法深入

NSFileManager的用法

- (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error;

获得path这个文件\文件夹的属性 

        //获取文件属性
        NSDictionary *dict = [fm attributesOfItemAtPath:fp error:nil];
        NSLog(@"%@",dict);

打印结果:

{
    NSFileCreationDate = "2015-10-07 02:25:12 +0000";
    NSFileExtensionHidden = 0;
    NSFileGroupOwnerAccountID = 20;
    NSFileGroupOwnerAccountName = staff;
    NSFileHFSCreatorCode = 0;
    NSFileHFSTypeCode = 0;
    NSFileModificationDate = "2015-10-07 02:25:12 +0000";
    NSFileOwnerAccountID = 501;
    NSFileOwnerAccountName = wjy0916;
    NSFilePosixPermissions = 420;
    NSFileReferenceCount = 1;
    NSFileSize = 411;
    NSFileSystemFileNumber = 1275199;
    NSFileSystemNumber = 16777221;
    NSFileType = NSFileTypeRegular;
}

- (NSArray *)subpathsAtPath:(NSString *)path; 

查找给定路径下的所有子路径,返回一个数组, 深度查找,不限于当前层,也会查找package的内容。 

注意:方法使用了递归方式

        //获取路径下所有子路径
        NSArray *array = [fm subpathsAtPath:fp2];
        NSLog(@"%@",array);

- (NSArray *)subpathsOfDirectoryAtPath:(NSString *)path error:(NSError **)error;

获得path的所有子路径(后代路径),上面方法功能一样。 

注意:此方法没有使用递归

        //获取路径下所有子路径
        NSArray *array = [fm subpathsOfDirectoryAtPath:fp2 error:nil];
        NSLog(@"%@",array);

- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error;

获得path的当前子路径(path下的所有直接子内容,path必须是一个目录) 

        //获取路径下所有子路径(不深度获取)
        NSArray *array = [fm contentsOfDirectoryAtPath:fp2 error:nil];
        NSLog(@"%@",array)

- (NSData *)contentsAtPath:(NSString *)path;

获得文件内容 

NSData *d = [fm contentsAtPath:fp];

- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error;

只能创建文件夹(createIntermediatesYES代表自动创建中间的文件夹)注意如果要创建的目录已经存在,则本次创建失败 

        isYes = [fm createDirectoryAtPath:fp2 withIntermediateDirectories:YES attributes:nil error:nil];
        if (isYes) {
            NSLog(@"目录创建成功");
        }

- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError**)error;
拷贝,如果目标目录已经存在同名文件,则无法拷贝 

        isYes = [fm copyItemAtPath:fp toPath:fp3 error:nil];
        if (isYes) {
            NSLog(@"拷贝成功");
        }
注意:此时两个路径都是文件路径,不能是目录路径。

- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError**)error;
移动(剪切

        isYes = [fm moveItemAtPath:fp toPath:fp3 error:nil];
        if (isYes) {
            NSLog(@"剪切成功");
        }
注意:此时两个路径都是文件路径,不能是目录路径。

- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error;

删除 

        isYes = [fm removeItemAtPath:fp3 error:nil];
        if (isYes) {
            NSLog(@"删除成功");
        }

























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值