在Objective-C 中的 Foundation 框架中,文件操作是由NSFileManager 类来实现的。

下面通过例子来说明如何创建一个文件,并向文件中写内容,以及如何读出文件中的内容:

- (void)testFileCreate
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
filePath = [filePath stringByAppendingPathComponent:@"new.txt"];
NSLog(@"filePath = %@",filePath);
// 判断文件是否存在
if (![fileManager fileExistsAtPath:filePath]){
// 若文件不存在,则新建文件
[fileManager createFileAtPath:filePath contents:nil attributes:nil];
}
// 向文件中写内容,通过文件句柄,NSFileHandle实现
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
NSString *content = @"hey,brother.This is a test.";
NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding];
[fileHandle writeData:contentData];
// 关闭文件
[fileHandle closeFile]; // 读取文件中的内容
fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *readData = [fileHandle readDataToEndOfFile];
// data 转 NSString
NSString *readStr = [[NSString alloc] initWithData:readData encoding:NSUTF8StringEncoding];
NSLog(@"readStr = %@",readStr);
[fileHandle closeFile];
// 直接以NSString 的方式读取文件
NSString *contentStr = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"contentStr = %@",contentStr);
}

文件的一些常规操作,如复制文件、删除文件、移动文件等:

- (void)testFileOperation
{
// 获得临时目录
NSString *tempPath = NSTemporaryDirectory();
NSLog(@"tempPath = %@",tempPath);
// 最后一级目录
NSLog(@"last = %@", [tempPath lastPathComponent] );
// 在最后增加一级目录,原目录不变,返回一个新的目录字符串
NSLog(@"add last = %@",[tempPath stringByAppendingPathComponent:@"add"]);
// 删除最后一级目录,原目录不变,返回一个新的目录字符串
NSLog(@"del last = %@",[tempPath stringByDeletingLastPathComponent]);
NSString *filePath = [tempPath stringByAppendingPathComponent:@"test.txt"];
NSLog(@"filePath = %@",filePath);
// 扩展名,输出为 txt
NSLog(@"extension = %@",[filePath pathExtension]);
NSFileManager *manager = [NSFileManager defaultManager];
if(![manager fileExistsAtPath:filePath]){
[manager createFileAtPath:filePath contents:nil attributes:nil];
} NSString *newPath = [tempPath stringByAppendingPathComponent:@"newtest.txt"];
// 拷贝文件
[manager copyItemAtPath:filePath toPath:newPath error:nil];
if([manager fileExistsAtPath:newPath]){
NSLog(@"copy success");
}
// 删除文件
[manager removeItemAtPath:newPath error:nil];
if(![manager fileExistsAtPath:newPath]){
NSLog(@"remove success");
} // 文件是否可读
if([manager isReadableFileAtPath:filePath]){
NSLog(@"readable");
}
// 文件是否可写
if([manager isWritableFileAtPath:filePath]){
NSLog(@"writeable");
}
}

Objective-C: NSFileManager 的使用的更多相关文章

  1. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  2. Objective-C( Foundation框架 一 NSFileManager)

    NSFileManager 用来管理文件系统的 它可以用于常见的文件,文件夹操作(拷贝,剪切,创建) NSFileManager使用了单例模式(Singleton) 使用defaultManager可 ...

  3. Objective C (iOS) for Qt C++ Developers(iOS开发,Qt开发人员需要了解什么?)

    Qt/C++开发人员眼中的Obj-C      对于我们第一次自己定义iOS应用来说,对于来自Qt/C++开发人员来说,我不得不学习Objective-C相关语法与知识 为了让读者可以更easy理解这 ...

  4. iOS开发核心语言Objective C —— 全部知识点总结

    本分享是面向有意向从事iOS开发的伙伴及苹果产品的发烧友,亦或是已经从事了iOS的开发人员,想进一步提升者.假设您对iOS开发有极高的兴趣,能够与我一起探讨iOS开发.一起学习,共同进步.假设您是零基 ...

  5. 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)

    ========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...

  6. ios 文件操作(NSFileManager)

    IOS的沙盒机制,应用只能访问自己应用目录下的文件,iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容. iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内. ...

  7. Objective C中的ARC的修饰符的使用---- 学习笔记九

    #import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...

  8. 【原】iOS学习之文件管理器(NSFileManager)和文件对接器(NSFileHandle)

    1.文件管理器(NSFileManager) 1> 主要作用及功能方法 主要作用:此类主要是对文件进行的操作(创建/删除/改名等)以及文件信息的获取. 功能方法: 2> 创建文件夹 创建所 ...

  9. Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法

    NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...

  10. [转] 从 C 到 Objective C 入门1

    转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...

随机推荐

  1. Hibernate映射集合属性

    Hibernate要求持久化集合属性字段必须声明为接口,实际的接口可以是java.util.Set,java.util.Collection,java.util.List,java.util.Map, ...

  2. main cannot be resolved or is not a field

    今天在做XML解析的时候,总是给我报 XML Parsing Error: XML or text declaration not at start of entity 的错误,后来查了下讲大概意思是 ...

  3. (任寒韬)WebApp群主 - MobileTech 资料

    web app : http://www.lightapp.cn/brand/index/4101 https://github.com/jtyjty99999/mobileTech/blob/mas ...

  4. IOS cocos2d笔记1

    结点添加.删除.获取1.结点:CCNode * childNode = [CCNode node]; 2.加入结点[myNode addChild:childNode z:0 tag:123];//z ...

  5. 再一次见证mssql中in 与exist的区别

    见下面代码 /*+' select * from '+@strDBName +'.dbo.m_aic where nodeid not in(select nodeid from @tmpAIC) ' ...

  6. acdream 1056 (黑白染色)

    题意:给你一些关系,每个关系是两只马的名字,表示这两个马不能在一个分组里,问你能否将这些马分成两组. 黑白染色,相邻的点染不同颜色.bfs搞即可,水题. /* * this code is made ...

  7. 从python run 和python unittest两种eclipse运行方式深入理解if __name__ == "__main__"

    在写一个简单的python测试程序的时候,发现eclipse中Run as "Python run 和 Python unittest”结果不一样?为什么会不一样? 先贴一下代码段: # - ...

  8. js控制不同的时间段显示不同的css样式

    js控制不同的时间段显示不同的css样式 js函数,可以放到单独的js文件中也可以放到当前页的<head>标记之内 function getCSS(){        datetoday ...

  9. js刷新页面方法

    1,reload 方法,该方法强迫浏览器刷新当前页面.语法:location.reload([bForceGet])   参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里取 ...

  10. Seam carving 学习笔记

    今天首次接触了图像编辑中的seam carving知识,感觉挺神奇的.虽然我自己可能理解的不是很深刻,但是记录下来,总是好的. seam carving直接翻译过来是“线裁剪”的意思.它的主要用途是对 ...