Objective-C: NSFileManager 的使用
在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 的使用的更多相关文章
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- Objective-C( Foundation框架 一 NSFileManager)
NSFileManager 用来管理文件系统的 它可以用于常见的文件,文件夹操作(拷贝,剪切,创建) NSFileManager使用了单例模式(Singleton) 使用defaultManager可 ...
- Objective C (iOS) for Qt C++ Developers(iOS开发,Qt开发人员需要了解什么?)
Qt/C++开发人员眼中的Obj-C 对于我们第一次自己定义iOS应用来说,对于来自Qt/C++开发人员来说,我不得不学习Objective-C相关语法与知识 为了让读者可以更easy理解这 ...
- iOS开发核心语言Objective C —— 全部知识点总结
本分享是面向有意向从事iOS开发的伙伴及苹果产品的发烧友,亦或是已经从事了iOS的开发人员,想进一步提升者.假设您对iOS开发有极高的兴趣,能够与我一起探讨iOS开发.一起学习,共同进步.假设您是零基 ...
- 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)
========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...
- ios 文件操作(NSFileManager)
IOS的沙盒机制,应用只能访问自己应用目录下的文件,iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容. iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内. ...
- Objective C中的ARC的修饰符的使用---- 学习笔记九
#import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...
- 【原】iOS学习之文件管理器(NSFileManager)和文件对接器(NSFileHandle)
1.文件管理器(NSFileManager) 1> 主要作用及功能方法 主要作用:此类主要是对文件进行的操作(创建/删除/改名等)以及文件信息的获取. 功能方法: 2> 创建文件夹 创建所 ...
- Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法
NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...
- [转] 从 C 到 Objective C 入门1
转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...
随机推荐
- UVa 10868 (物理) Bungee Jumping
题意: 有个人在蹦极,给出悬崖的高度,绳子的长度,弹簧绳的胡克系数 以及 人的质量. 判断人是否能够着地,能的话是否能安全着地.所谓安全着地就是到达地面的速度不超过10m/s. 分析: 学过一点高中物 ...
- POJ 2752 (KMP 所有可能长度的前缀后缀) Seek the Name, Seek the Fame
题意: 求一个字符串的相同前缀后缀的所有可能的长度,这里该字符串其本身也算自己的前缀和后缀. 分析: 我们知道next数组的性质是,该字符之前的字符串的最大相同前缀后缀. 既然知道了最大的,即next ...
- main cannot be resolved or is not a field
今天在做XML解析的时候,总是给我报 XML Parsing Error: XML or text declaration not at start of entity 的错误,后来查了下讲大概意思是 ...
- Can't dispatch DDM chunk 52454151: no handler defined
[2010-07-12 10:10:06 - Hello Google Android]ActivityManager: DDM dispatch reg wait timeout [2010-07- ...
- PHP单元测试工具PHPUnit初体验
今天接到了个任务,需要对数字进行计算,因为涉及到整数,小数,和科学计数法等很多条件,所以人工测试非常麻烦,于是想到了PHP的单元测试工具PHPUnit,所以写个文档备查. 看了PHPUnit的文档之后 ...
- Java [Leetcode 137]Single Number II
题目描述: Given an array of integers, every element appears three times except for one. Find that single ...
- ECshop 二次开发模板教程3
<p>商品列表</p> <table width="70%" border="1"> <tr> <td&g ...
- POJ 1080 Human Gene Functions
题意:给两个DNA序列,在这两个DNA序列中插入若干个'-',使两段序列长度相等,对应位置的两个符号的得分规则给出,求最高得分. 解法:dp.dp[i][j]表示第一个字符串s1的前i个字符和第二个字 ...
- gitlab的使用
Gitlab的使用 最近成功的在公司部署了gitlab,鉴于同学们还不会使用,这里写篇博客说明下.如果想安装gitlab的话,需要一些linux的基础知识,我在这里记录了我安装的参考<http: ...
- Android常用控件之FragmentTabHost的使用
最近在学TabHost时发现TabActivity在API level 13以后不用了,所以就去寻找它的替换类,找到FragmentActivity,可以把每个Fragment作为子tab添加到Fra ...