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语法了.不过,了解之后才发现,原 ...
随机推荐
- BZOJ3858: Number Transformation
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3858 题解:设第i个数为i*a;第i+1个数为(i+1)*b.则(i+1)*b>i*a; ...
- 多线程程序设计学习(3)immutable pattern模式
Immutable pattern[坚不可摧模式] 一:immutable pattern的参与者--->immutable(不变的)参与者 1.1:immutable参与者是一个 ...
- sharepoint SPFolder的使用
转:http://blog.csdn.net/pclzr/article/details/7591731 SPFolder是SharePoint对象模型中文件夹相关的类,它的使用方法相对比较简单.获取 ...
- [android]如何让TextView使用超链接
找了很多网址,最后是这个有说到. 总的做法是: 1.(当然也可以从Res中获取.) tv.setText(Html.fromHtml("<a href=\"http://ww ...
- [Everyday Mathematics]20150106
(1). 设 $f\in C[0,T]$, $g$ 是 $T$-周期函数, 试证: $$\bex \vlm{n}\int_0^T f(x)g(nx)\rd x=\frac{1}{T}\int_0^T ...
- hdu 1544 水题
水题 /* * Author : ben */ #include <cstdio> #include <cstdlib> #include <cstring> #i ...
- ASP.NET Session丢失的解决方案
正常操作情况下会有ASP.NET Session丢失的情况出现.因为程序是在不停的被操作,排除Session超时的可能.另外,Session超时时间被设定成60分钟,不会这么快就超时的.现在我就把原因 ...
- 【JSONCpp】简介及demo
一.JSON简介 JSON 一种轻量级的数据交换格式,易于阅读.编写.解析,全称为JavsScript ObjectNotation. JSON由两种基本结构组成 ① 名字/值 对的集合,可以理解 ...
- sqlServer 取每组的前几条数据
首先的建表语句: ) DROP TABLE [test] CREATE TABLE [test] ( [id] [, ) NOT NULL , [name] [nvarchar] () NULL , ...
- mysql 中的外键key值的详解
如果Key是空的, 那么该列值的可以重复, 表示该列没有索引, 或者是一个非唯一的复合索引的非前导列2. 如果Key是PRI, 那么该列是主键的组成部分3. 如果Key是UNI, 那么该列是一个唯 ...