一、简单展示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 ;
}
//检测两个文件内容是否相同
if ([fm contentsEqualAtPath: fname andPath: @"newFile"] == NO) {
NSLog(@"\nThe contents is not same");
return ;
}
//移动或者改名文件
if ([fm moveItemAtPath: @"newFile" toPath: @"myFile2" error:NULL] == NO) {
NSLog(@"\nCan't change the name");
return ;
}
//获取文件数据字典
if ((attr = [fm attributesOfItemAtPath: fname error:NULL]) == nil) {
NSLog(@"\nGet attributets failed");
return ;
}
//文件大小
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 ;
}

二、通过NSData完成副本制作

 int main(int argc, const char * argv[])
{ @autoreleasepool {
//通过NSDate来完成文件副本制作
NSFileManager *fm = [NSFileManager defaultManager];
NSData *dt; dt = [fm contentsAtPath: @"myfile"]; if (dt == nil) {
NSLog(@"Read file failed....");
return ;
} //将缓冲区NSData中的内容复制到文件中
if ([fm createFileAtPath:@"myFavoriteFile" contents: dt attributes:nil] == NO) {
NSLog(@"Creat backups failed");
return ;
} //读出文件内容
NSLog(@"\n%@", [NSString stringWithContentsOfFile:@"myFavoriteFile" encoding: NSUTF8StringEncoding error:NULL]);
}
return ;
}

三、简单的目录操作

 #import <Foundation/Foundation.h>

 int main(int argc, const char * argv[])
{ @autoreleasepool {
NSString *newDir = @"newDir";
NSString *currentPath;
NSFileManager *fm = [NSFileManager defaultManager]; //获取当前路径
currentPath = [fm currentDirectoryPath];
NSLog(@"\nCurrentpath is : \n%@", currentPath); //在当前目录下新建一个目录
if ([fm createDirectoryAtPath:newDir withIntermediateDirectories:TRUE attributes:nil error:NULL] == NO) {
NSLog(@"\nCouldn't creat the directory...");
return ;
} //更改路径名
if ([fm moveItemAtPath: newDir toPath: @"changeDir" error:NULL] == NO) {
NSLog(@"\nChange directory name failed");
return ;
} //更改当前路径
if ([fm changeCurrentDirectoryPath:@"changeDir"] == NO) {
NSLog(@"\nChange current directory failed");
return ;
}
NSLog(@"\nAfter change current directory.....");
currentPath = [fm currentDirectoryPath];
NSLog(@"\nCurrentpath is : \n%@", currentPath);
}
return ;
}

最新文章

  1. iOS_常用C语言函数
  2. Android进程间的通信之Messenger
  3. SwiftLint——Swift代码检查及自动格式化工具
  4. Mac下开启FTPserver
  5. GPUImage相关(转)
  6. Apache ab并发负载压力测试
  7. python exec
  8. 国外PHP学习网站书籍资料汇总
  9. mysql router 自动failover测试
  10. ERROR 1045: Access denied for user: &#39;root@localhost&#39; (Using password: YES)(转)
  11. PHP session 跨子域问题总结
  12. Linux UDEV和为MySQL InnoDB共享表空间配置裸设备
  13. AJAX 表单提交 文件上传
  14. 自定义alert和confirm
  15. Maven入门知识介绍
  16. 前端项目中常用es6知识总结 -- 箭头函数及this指向、尾调用优化
  17. Python Selenium set Chrome Preference Download Location.
  18. 关于电脑重装win10系统导致编译环境失效(jdk)
  19. Vue页面跳转$router.push 的用法
  20. 仿照hibernate封装的一个对数据库操作的jdbc工具类

热门文章

  1. 提高Delphi的编译速度(bpl和bcp)
  2. poj2000---和1969一样的模板
  3. enumerate小技巧和列表推导式
  4. 关于json文本数据的一些使用方法
  5. 集合的实现 -- 数据结构与算法的javascript描述 第九章
  6. html中radio,checkbox值的获取、赋值、注册事件
  7. Samba在CentOS下的图形化界面的安装
  8. 浅谈SpringMVC(一)
  9. istringstream和ostringstream的使用方法
  10. Spark源码学习3