Foundation 框架 NSFileManager,NSData 简单的文件操作

一、简单展示NSFileManager的使用

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        //创建文件管理对象
        NSFileManager *fm = [NSFileManager defaultManager];
        //要操作的文件名
        NSString *fname = @"myfile";
        //获取文件的字典
        NSDictionary *attr;
        //当前路径
        NSString *path;
        //获取当前路径
        path = [fm currentDirectoryPath];
        //NSLog(@"\nThe current path is : %@", path);
        
        //检测文件是否存在
        if ([fm fileExistsAtPath: fname] == NO) {
            //如果不存在则建立一个文件
            [fm createFileAtPath: fname contents: NULL attributes:nil];
            //NSLog(@"\nThe file is not exist!");
            //return 0;
        }
        //拷贝创建一个新文件, 新文件若已存在则报错
        if ([fm copyItemAtPath: fname toPath: @"newFile" error: NULL] == NO) {
            NSLog(@"\n Can't copy the file");
            return 1;
        }
        //检测两个文件内容是否相同
        if ([fm contentsEqualAtPath: fname andPath: @"newFile"] == NO) {
            NSLog(@"\nThe contents is not same");
            return 2;
        }
        //移动或者改名文件
        if ([fm moveItemAtPath: @"newFile"  toPath: @"myFile2" error:NULL] == NO) {
            NSLog(@"\nCan't change the name");
            return 3;
        }
        //获取文件数据字典
        if ((attr = [fm attributesOfItemAtPath: fname error:NULL]) == nil) {
            NSLog(@"\nGet attributets failed");
            return 4;
        }
        //文件大小
        NSLog(@"%@", attr[NSFileSize]);
        //文件类型
        NSLog(@"%@", attr[NSFileType]);
        //创建者
        NSLog(@"%@", attr[NSFileOwnerAccountName]);
        //
        NSLog(@"%@", attr[NSFileCreationDate]);
        //显示文件内容
        NSLog(@"\n Show the file contents");
        NSLog(@"\n%@", [NSString stringWithContentsOfFile: fname encoding:NSUTF8StringEncoding error:NULL]);
    }
    return 0;
}

 二、通过NSData完成副本制作

 1 int main(int argc, const char * argv[])
 2 {
 3 
 4     @autoreleasepool {
 5         //通过NSDate来完成文件副本制作
 6         NSFileManager *fm = [NSFileManager defaultManager];
 7         NSData *dt;
 8         
 9         dt = [fm contentsAtPath: @"myfile"];
10         
11         if (dt == nil) {
12             NSLog(@"Read file failed....");
13             return 0;
14         }
15         
16         //将缓冲区NSData中的内容复制到文件中
17         if ([fm createFileAtPath:@"myFavoriteFile" contents: dt attributes:nil] == NO) {
18             NSLog(@"Creat backups failed");
19             return 1;
20         }
21         
22         //读出文件内容
23         NSLog(@"\n%@", [NSString stringWithContentsOfFile:@"myFavoriteFile" encoding: NSUTF8StringEncoding error:NULL]);
24     }
25     return 0;
26 }

三、简单的目录操作

 1 #import <Foundation/Foundation.h>
 2 
 3 int main(int argc, const char * argv[])
 4 {
 5 
 6     @autoreleasepool {
 7         NSString *newDir = @"newDir";
 8         NSString *currentPath;
 9         NSFileManager *fm = [NSFileManager defaultManager];
10         
11         //获取当前路径
12         currentPath = [fm currentDirectoryPath];
13         NSLog(@"\nCurrentpath is : \n%@", currentPath);
14         
15         //在当前目录下新建一个目录
16         if ([fm createDirectoryAtPath:newDir withIntermediateDirectories:TRUE attributes:nil error:NULL] == NO) {
17             NSLog(@"\nCouldn't creat the directory...");
18             return 0;
19         }
20         
21         //更改路径名
22         if ([fm moveItemAtPath: newDir toPath: @"changeDir" error:NULL] == NO) {
23             NSLog(@"\nChange directory name failed");
24             return 2;
25         }
26         
27         //更改当前路径
28         if ([fm changeCurrentDirectoryPath:@"changeDir"] == NO) {
29             NSLog(@"\nChange current directory failed");
30             return 1;
31         }
32         NSLog(@"\nAfter change current directory.....");
33         currentPath = [fm currentDirectoryPath];
34         NSLog(@"\nCurrentpath is : \n%@", currentPath);
35     }
36     return 0;
37 }

 

转载于:https://www.cnblogs.com/pretty-guy/p/3957603.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值