1,引用和使用文件

NSFileManager 是一个单例对象,在mac应用中可以获取任何地址,在IOS中获取的是相对应的应用程序的地址。可以使用 defaultManager 来得到当前应用程序地址,例如。

NSFileManager *fileManager = [NSFileManager defaultManager];

模拟器目录 /Users/username/Library/Application Support/iPhone Simulator/

一旦有了参考,你便可以得到相应目录,例如你想得到currentDirectoryPath

目录。

NSString *currentDirectoryPath = [fileManager currentDirectoryPath];

想切换目录,用changeCurrentDirectoryPath,如

[fileManager changeCurrentDirectoryPath:@"/Users/Shared”];

代码例子。

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *currentDirectoryPath = [fileManager currentDirectoryPath];

NSLog(@"currentDirectoryPath = %@---", currentDirectoryPath);

[fileManager changeCurrentDirectoryPath:@"/Users/Shared"];

currentDirectoryPath = [fileManager currentDirectoryPath];

NSLog(@"currentDirectoryPath = %@+++", currentDirectoryPath);

2,mac目录引用

使用NSSearchPathForDirectoriesInDomains

例如

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];

NSLog(@"%@++", bundlePath);

打印的值大概是

/Users/guanliyang/Library/Developer/Xcode/DerivedData/filePath-dgfhcnomuvxhzpepzbrxbrfkhkkz/Build/Products/Debug++

如果想得到文档目录

NSString *directoryPathName = [NSSearchPathForDirectoriesInDomains

(NSDocumentDirectory,NSAllDomainsMask, YES) lastObject];

更多参数请参考原著objective-C recipes。

3,获取IOS引用目录

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];

NSLog(@"%@", bundlePath);

这个得到的是你IOS应用的主目录,/Users/guanliyang/Library/Application Support/iPhone Simulator/7.1/Applications/1A888392-101B-40B1-A47A-195C19DBC18E/aa.app

如果你想得到模拟器的文档目录,你可以这样做

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSLog(@"%@", documentsDirectory);

还有其他几个选项,IOS模拟器默认生成的几个目录

NSDocumentDirectory  Location for user-generated content (read-write, backed up) The

NSLibraryDirectory  app’s library directory (read-write, backed up)

NSCachesDirectory  A directory for cached files (read-write, not backed up)

默认只生成这三个目录。

4,获取文件属性。

先指定文件

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *filePathName = @"/Users/Shared/textfile.txt”;

声明error

NSError *error = nil;

给定文件路径,试着读取文件并查看是否有错。

NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:filePathName error:&error];

如果没有错误,获取文件相关信息。

if(!error){

NSDate *dateFileCreated = [fileAttributes valueForKey:NSFileCreationDate];

NSString *fileType = [fileAttributes valueForKey:NSFileType];

}

还有很多相关信息可打印,比如大小,所属用户,文件个数等等....

5,获取文件子目录和目录中的目录。

先得到文件列表

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *sharedDirectory = @"/Users/Shared”;

NSError *error = nil;

NSArray *listOfFiles = [fileManager contentsOfDirectoryAtPath:sharedDirectory

error:&error];

递归目录,可以使用subpathsOfDirectoryAtPath:error

例如

NSArray *listOfSubPaths = [fileManager subpathsOfDirectoryAtPath:sharedDirectory

error:&error];

例子代码

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

{

@autoreleasepool {

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *sharedDirectory = @"/Users/Shared";

NSError *error = nil;

NSArray *listOfFiles = [fileManager contentsOfDirectoryAtPath:sharedDirectory

error:&error];

if(!error)

NSLog(@"Contents of shared directory: %@", listOfFiles);

NSArray *listOfSubPaths = [fileManager subpathsOfDirectoryAtPath:sharedDirectory

error:&error];

if(!error)

NSLog(@"Sub Paths of shared directory”: %@", listOfSubPaths);

}

return 0;

}

6,管理目录

用代码操作目录。

使用NSFileManager,

createDirectoryAtPath:withIntermediateDirectories:attributes:error:可以创建目录,

moveItemAtPath:toPath:error: 可以移动目录,

removeItemAtPath:error:删除目录,

copyItemAtPath:toPath:error:拷贝目录

具体代码

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *sharedDirectory = @"/Users/Shared/NewDirectory1/NewSubDirectory1";

NSError *error = nil;

创建

BOOL directoryCreated = [fileManager createDirectoryAtPath:newDirectory

withIntermediateDirectories:YES

attributes:nil

error:&error];

该函数返回布尔值,提示是否创建成功。

同理,移动文件。

NSString *directoryMovedTo = @"/Users/Shared/NewSubDirectory1";

BOOL directoryMoved = [fileManager moveItemAtPath:newDirectory

toPath:directoryMovedTo

error:&error];

删除目录

NSString *directoryToRemove = @"/Users/Shared/NewDirectory1";

BOOL directoryRemoved =[fileManager removeItemAtPath:directoryToRemove

error:&error];

复制目录。

NSString *directoryToCopy = @"/Users/Shared/NewSubDirectory1";

NSString *directoryToCopyTo = @"/Users/Shared/CopiedDirectory";

BOOL directoryCopied =[fileManager copyItemAtPath:directoryToCopy

toPath:directoryToCopyTo

error:&error];

具体代码例子。

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *newDirectory =@"/Users/Shared/NewDirectory1/NewSubDirectory1";

NSError *error = nil;

BOOL directoryCreated = [fileManager createDirectoryAtPath:newDirectory

withIntermediateDirectories:YES

attributes:nil

error:&error];

if(!error)

NSLog(@"directoryCreated = %i with no error", directoryCreated);

else

NSLog(@"directoryCreated = %i with error %@", directoryCreated, error);

NSString *directoryMovedTo = @"/Users/Shared/NewSubDirectory1";

BOOL directoryMoved = [fileManager moveItemAtPath:newDirectory

toPath:directoryMovedTo

error:&error];

if(!error)

NSLog(@"directoryMoved = %i with no error", directoryMoved);

else

NSLog(@"directoryMoved = %i with error %@", directoryMoved, error);

NSString *directoryToRemove = @"/Users/Shared/NewDirectory1";

BOOL directoryRemoved =[fileManager removeItemAtPath:directoryToRemove

error:&error];

if(!error)

NSLog(@"directoryRemoved = %i with no error", directoryRemoved);

else

NSLog(@"directoryRemoved = %i with error %@", directoryRemoved, error);

NSString *directoryToCopy = @"/Users/Shared/NewSubDirectory1";

NSString *directoryToCopyTo = @"/Users/Shared/CopiedDirectory";

BOOL directoryCopied =[fileManager copyItemAtPath:directoryToCopy

toPath:directoryToCopyTo

error:&error];

if(!error)

NSLog(@"directoryCopied = %i with no error", directoryCopied);

else

NSLog(@"directoryCopied = %i with error %@", directoryCopied, error);

7,管理文件。

添加,移动,复制,删除文件。

代码例子,图片不能访问可新找一图片地址。

NSFileManager *fileManager = [NSFileManager defaultManager];

NSURL *url = [NSURL URLWithString:@"http://img5.imgtn.bdimg.com/it/u=3163907393,1865504758&fm=23&gp=0.jpg”];

用NSData为图片对象,

NSData *dataObject = [NSData dataWithContentsOfURL:url];

创建图片

NSString *newFile = @"/Users/Shared/apples-oranges.jpg";

BOOL fileCreated = [fileManager createFileAtPath:newFile

contents:dataObject

attributes:nil];

移动图片

NSError *error = nil;

NSString *fileMovedTo = @"/Users/Shared/apples-oranges-moved.jpg";

BOOL fileMoved = [fileManager moveItemAtPath:newFile

toPath:fileMovedTo

error:&error];

删除图片

NSString *fileToRemove = @"/Users/Shared/apples-oranges-moved.jpg";

BOOL fileRemoved =[fileManager removeItemAtPath:fileToRemove

error:&error];

复制图片

NSString *fileToCopy = @"/Users/Shared/apples-oranges-moved.jpg";

NSString *copiedFileName = @"/Users/Shared/apples-oranges-backup-copy.jpg";

BOOL fileCopied = [fileManager copyItemAtPath:fileToCopy

toPath:copiedFileName

error:&error];

具体代码例子。

NSFileManager *fileManager = [NSFileManager defaultManager];

NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/img/baidu_sylogo1.gif"];

NSData *dataObject = [NSData dataWithContentsOfURL:url];

NSString *newFile = @"/Users/Shared/apples-oranges.jpg";

BOOL fileCreated = [fileManager createFileAtPath:newFile

contents:dataObject

attributes:nil];

NSLog(@"fileCreated = %i with no error", fileCreated);

NSError *error = nil;

NSString *fileMovedTo = @"/Users/Shared/apples-oranges-moved.jpg";

BOOL fileMoved = [fileManager moveItemAtPath:newFile

toPath:fileMovedTo

error:&error];

if(!error)

NSLog(@"fileMoved = %i with no error", fileMoved);

else

NSLog(@"fileMoved = %i with error %@", fileMoved, error);

NSString *fileToCopy = @"/Users/Shared/apples-oranges-moved.jpg";

NSString *copiedFileName = @"/Users/Shared/apples-oranges-backup-copy.jpg";

BOOL fileCopied = [fileManager copyItemAtPath:fileToCopy

toPath:copiedFileName

error:&error];

if(!error)

NSLog(@"fileCopied = %i with no error", fileCopied);

else

NSLog(@"fileCopied = %i with error %@", fileCopied, error);

NSString *fileToRemove = @"/Users/Shared/apples-oranges-moved.jpg";

BOOL fileRemoved =[fileManager removeItemAtPath:fileToRemove

error:&error];

if(!error)

NSLog(@"fileRemoved = %i with no error", fileRemoved);

else

NSLog(@"fileRemoved = %i with error %@", fileRemoved, error);

可以更改容易查看目录做具体测试。

8,检查文件状态。

常用方法。

 fileExistsAtPath:

 isReadableFileAtPath:

 isWritableFileAtPath:

 isExecutableFileAtPath:  isDeletableFileAtPath:.

检查文件是否存在。

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *filePathName = @"/Users/Shared/textfile.txt”;

BOOL fileExists = [fileManager fileExistsAtPath:filePathName];

文件是否可读

BOOL fileIsReadable = [fileManager isReadableFileAtPath:filePathName];

是否可写

BOOL fileIsWriteable = [fileManager isWritableFileAtPath:filePathName];

最后,文件是否可删除。

BOOL fileIsDeleteable = [fileManager isDeletableFileAtPath:filePathName];

具体代码例子。

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *filePathName = @"/Users/Shared/textfile.txt";

BOOL fileExists = [fileManager fileExistsAtPath:filePathName];

if(fileExists)

NSLog(@"%@ exists", filePathName);

else

NSLog(@"%@ doesn't exist", filePathName);

BOOL fileIsReadable = [fileManager isReadableFileAtPath:filePathName];

if(fileIsReadable)

NSLog(@"%@ is readable", filePathName);

else

NSLog(@"%@ isn't readable", filePathName);

BOOL fileIsWriteable = [fileManager isWritableFileAtPath:filePathName];

if(fileIsWriteable)

NSLog(@"%@ is writable", filePathName);

else

NSLog(@"%@ isn't writable", filePathName);

BOOL fileIsExecutable = [fileManager isExecutableFileAtPath:filePathName];

if(fileIsExecutable)

NSLog(@"%@ is an executable", filePathName);

else

NSLog(@"%@ isn't an executable", filePathName);

BOOL fileIsDeleteable = [fileManager isDeletableFileAtPath:filePathName];

if(fileIsDeleteable)

NSLog(@"%@ is deletable", filePathName);

else

NSLog(@"%@ isn't an deletable", filePathName);

没文件可以自己touch创建一个。

9,更改文件属性。

文件管理对象,文件,错误对象。

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *filePathName = @"/Users/Shared/textfile.txt";

NSError *error = nil;

建立一个字典数组以便获取文件属性列表。

NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];

[attributes setObject:[NSDate date] forKey:NSFileModificationDate];

我们只改一下文件的修改日期

BOOL attributeChanged = [fileManager setAttributes:attributes

ofItemAtPath:filePathName

error:&error];

具体代码

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *filePathName = @"/Users/Shared/textfile.txt";

NSError *error = nil;

//Get the file attributes so you can compare later on:

NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:filePathName

error:&error];

if(!error)

NSLog(@"%@ file attributes (before): %@",filePathName, fileAttributes);

NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];

[attributes setObject:[NSDate date] forKey:NSFileModificationDate];

BOOL attributeChanged = [fileManager setAttributes:attributes

ofItemAtPath:filePathName

error:&error];

if(error)

NSLog(@"There was an error: %@", error);

else{

NSLog(@"attributeChanged = %i", attributeChanged);

//Get the file attributes to see the change:

NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:filePathName

error:&error];

if(!error)

NSLog(@"%@ file attributes (after): %@",filePathName, fileAttributes);

最新文章

  1. Servlet 生命周期与web容器的关系
  2. 性能测试总结工作总结-基于WebService协议脚本 内置函数手动编写
  3. java——多线程——内部类共享同一个外部类对象的成员变量
  4. 轻松自动化---selenium-webdriver(python) (一)
  5. 重写Object类中的equals方法
  6. bzoj 2761 平衡树
  7. 探索 Linux 内存模型--转
  8. unity 导出 android安装包配置方案
  9. 枚举的基本使用方法 Enumerations
  10. Android 按钮按下效果
  11. 【Lotus Notes】邮件获取
  12. UVA 10308 Roads in the North
  13. Spring mvc系列一之 Spring mvc简单配置
  14. cookie值的设置,获取及删除
  15. 关于链表所有操作,面试必考C++
  16. vcenter server appliance(vcsa) 配置IP的方法
  17. 浅谈压缩感知(六):TVAL3
  18. 安全管理中心(SOC)引导企业信息安全建设的思路
  19. Python 操作Excel之通过xlutils实现在保留原格式的情况下追加写入数据
  20. 【转】【Flex】FLEX 学习网站分享

热门文章

  1. 20个命令行工具监控 Linux 系统性能
  2. 在node.js中使用ejs的demo 第五篇
  3. JS输出当前时间,且每秒变化
  4. 关于arguments.callee的用途
  5. php页面相互调用的知识点
  6. C#access数据库操作
  7. ASP.NET MVC3使用Unity2.0实现依赖注入(转载和扩展)
  8. 关于Apple Pay,一篇让你不看就会后悔的文章
  9. 鼠标进入与离开的消息(覆盖CM_MOUSEENTER与CM_MOUSELEAVE消息)——Windows本身没有这样的消息
  10. Nginx和Nginx+的比较(上)